Tuesday, July 28, 2009

Creating simple form in Zend Framwork




Zend frame work has a component called zend_form used for creating forms.
It simplify the process of form validation, data filtering, and data storage.
To create a simple form in zend follow the steps:

1- Create a simple form class and place it in application/forms/Simple_Form.
2- Add some element in the Simple_Form class.
3- Create a controller for the Simple_Form using the following command.

(your project path)#zf.sh create controller simple

It will be created in application/controllers/mycontroller.php. It will also create
a folder name 'simple' in application/view/scripts/simple/index.phtml

4- You can also create actions in the controller by using following command.

(your project path)#zf.sh create action myaction simple

After creating an action it would place a file name 'myaction.phtml' in
application/view/scripts/simple/myaction.phtml

Now place an object of your form class in this action.

public function myaction() {

$form = new Simple_Form();
$this->view->form = $form;

}

5- To see the form place the following line in the myaction.phtml file.

$echo $this->form;