Home Tutorial Php Zendframework Zend FrameWork Part-10

 
 

Share on Google+Share on Google+
Zend FrameWork Part-10
Posted on: April 19, 2010 at 12:00 AM
Advertisement
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:

  • Display a form to add new title, author, and publisher
  • Submit the form data into the database.

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));

}

}


.../application/controllers/IndexController.php

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:

add new book

 

Advertisement

Related Tags for Zend FrameWork Part-10:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 19, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com