In this tutorial we will study about Zend Framwork of PHP and in this current tutorial we will study how to add new books etc.. This is the tenth episode of the series subsequent pages will discuss on other topic like how to delete, edit etc.
In this tutorial we will study about Zend Framwork of PHP and in this current tutorial we will study how to add new books etc.. This is the tenth episode of the series subsequent pages will discuss on other topic like how to delete, edit etc.Zend Framework-10:
Adding New Books:
Now we will create forms to add new books, There will be two parts to this part:
Zend_Form component will be used in this tutorial to create the form to add new book details. We will use zf command to create the form:
zf create form Book
The above command create a new file called Book.php in application/forms. Now copy the following lines and remove the contents of Book.php by it.
.../application/form/Book.php:
<?php
class Application_Form_Book extends Zend_Form
{
public function __construct($options = null){
parent::__construct($options);
$this->setName('book');
$id=new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$author=new Zend_Form_Element_Text('author');
$author->setLabel('Author')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$publisher=new Zend_Form_Element_Text('publisher');
$publisher->setLabel('Publisher')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$title=new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim');
$submit=new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id','submitbutton');
$this->addElements(array($id,$author,$publisher,$title,$submit));
}
}
public function addAction()
{
// action body
$this->view->title="Add Any Book";
$this->view->headTitle($this->view->title);
$form=new Application_Form_Book();
$form->submit->setLabel('Add');
$this->view->form=$form;
if($this->getRequest()->isPost())
{
$formData=$this->getRequest()->getPost();
if($form->isValid($formData))
{
$author=$form->getValue('author');
$publisher=$form->getValue('publisher');
$title=$form->getValue('title');
$book=new Application_Model_DbTable_Books();
$book->addBook($author,$publisher,$title);
$this->_helper->redirector('index');
}
else{
$form->populate($formData);
}
}
}
../application/views/scripts/index/add.phtml
<?php echo $this->form;?>
In the constructor of Application_Form_Book, we need to create five form data elements for id, author, publisher, title, and for the submit button respectively. For each individual item we will set attributes, and the label which will have significant meaning for that data item. ZF provides various filter and Int is one of them with the help of this filter we can prevent SQL injection issues.
For the next text elements we will add StripTags and StringTrim filters, which will remove unwanted HTML and whitespaces. Now IndexController's addAction will display the form and take the necessary action to submit the form.
Output: