Zend FrameWork Part-10


 

Zend FrameWork Part-10

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:

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

0

$title=new Zend_Form_Element_Text('title');

$title->setLabel('Title')

1

->setRequired(true)

->addFilter('StripTags')

->addFilter('StringTrim');

2

$submit=new Zend_Form_Element_Submit('submit');

$submit->setAttrib('id','submitbutton');

3

$this->addElements(array($id,$author,$publisher,$title,$submit));

}

4

}


.../application/controllers/IndexController.php

public function addAction()

5

{

// action body

$this->view->title="Add Any Book";

6

$this->view->headTitle($this->view->title);

$form=new Application_Form_Book();

7

$form->submit->setLabel('Add');

$this->view->form=$form;

8

if($this->getRequest()->isPost())

{

$formData=$this->getRequest()->getPost();

9

if($form->isValid($formData))

{

$author=$form->getValue('author');

0

$publisher=$form->getValue('publisher');

$title=$form->getValue('title');

$book=new Application_Model_DbTable_Books();

1

$book->addBook($author,$publisher,$title);

$this->_helper->redirector('index');

}

2

else{

$form->populate($formData);

}

3

}

}

4

../application/views/scripts/index/add.phtml

<?php echo $this->form;?>

 

5

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:

6

add new book

 

Ads