form validation using zend framework

Zend framework is one of the most popular MVC framework based on php. It has lots of feature listed below
1. Model-View-Controller (MVC)
2. Database adapters
3. Form validatiors
4. Web Services
5. User Authenication and Authorisation.

Form Validation is important component to handle, to ensure required fields are filtered, before the data is store in the database. But in this example data is not stored in the database.

Lets get started. Here i have grouped all the class to ‘default’ module and placed in main ‘modules’ in applcation folder.

To demostrate the form validation I have created a ExampleController with ‘form’ action to process the form validation and once validated it is redirected to example/success page.

class ExampleController extends Zend_Controller_Action
public function indexAction(){

public function formAction()
$form = new Default_Form_Example;
$this->view->form = $form;
// check the request
// run the validators
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
// valid data: get the filtered and valid values
// do something, save to database or write to file
// display a success view
$values = $form->getValues();
} else {
// invalid data: get the error message array
// for manual processing (if needed)
// redisplay the form with errors
$this->view->messages = $form->getMessages();

public function successAction(){


Below the Default_Form_Example, where i have created the form element and set the validation rules. It reside in ‘default/forms’ folder

class Default_Form_Example extends Zend_Form
public function init()

// initialize form
// create text input for age
// should contain only integer values between 1 and 100
$age = new Zend_Form_Element_Text(‘age’);
->setOptions(array(‘size’ => ‘4’))
->addValidator(new Zend_Validate_Int())
->addValidator(new Zend_Validate_Between(1,100));

// create text input for name
// should contain only alphabetic characters and whitespace
$name = new Zend_Form_Element_Text(‘fname’);
$name->setLabel(‘First name:’)
->setOptions(array(‘size’ => ’16’))
->addValidator(new Zend_Validate_Alpha(true));

// create text input for email address
// should contain a valid email address
$email = new Zend_Form_Element_Text(’email’);
$email->setLabel(‘Email address:’)
->setOptions(array(‘size’ => ’16’))
->addValidator(new Zend_Validate_EmailAddress());

// create submit button
$submit = new Zend_Form_Element_Submit(‘submit’);
$submit->setLabel(‘Sign Up’);

// attach elements to form

form view file in created in ‘default\views\scripts\example’ folder. Here we can display the form using the code ‘ echo $this->form; ‘ but i prefer to normal html form to control the form layout.
<form enctype=”application/x-www-form-urlencoded” action=”/example/form” method=”post”>
<label for=”age”>Age:</label>
<input type=”text” name=”age” id=”age” value=”<?php echo $this->form->getValue(‘age’); ?>” size=”4″>
if( isset($this->messages[‘age’][‘isEmpty’]) )
echo $this->messages[‘age’][‘isEmpty’] ;
if( isset($this->messages[‘age’][‘notInt’]) )
echo $this->messages[‘age’][‘notInt’] ;
if( isset($this->messages[‘age’][‘notBetween’]) )
echo $this->messages[‘age’][‘notBetween’];
<label for=”name”>First name:</label>
<input type=”text” name=”fname” id=”fname” value=”<?php echo $this->form->fname->getValue(); ?>” size=”16″>
if( isset($this->messages[‘fname’][‘isEmpty’]) )
echo $this->messages[‘fname’][‘isEmpty’] ;
if( isset($this->messages[‘fname’][‘notAlpha’]) )
echo $this->messages[‘fname’][‘notAlpha’] ;

<label for=”email”>Email:</label>
<input type=”text” name=”email” id=”email” value=”<?php echo $this->form->getValue(’email’); ?>”” size=”16″>
if( isset($this->messages[’email’][‘isEmpty’]) )
echo $this->messages[’email’][‘isEmpty’] ;
if( isset($this->messages[’email’][’emailAddressInvalidFormat’]) )
echo $this->messages[’email’][’emailAddressInvalidFormat’] ;

<input type=”submit” name=”submit” id=”submit” value=”Sign Up”>




Download code

vikram viswani ‘A begginners guide Zend framework’

Posted in Uncategorized | Tagged | Leave a comment

Hello world!

Welcome to This is your first post. Edit or delete it and start blogging!


Posted in Uncategorized | 1 Comment