Tuesday, 13 January 2015

Zend Form part 2

Lets see how things work in Zend Forms;
First we Create an object of Zend_Form;

$form = new Zend_Form;

Than we set the Action to where we want to post the form by calling the setAction() method 
and setting the Method to POST.
$form->setAction('/resource/process')
     ->setMethod('post');

Than we create a TEXT field by and set the LABEL to First name
$firstName = new Zend_Form_Element_Text('firstName');

we added some other features to the TextField like it is required and cannot be empty 
        $firstName->setLabel('First name')
                  ->setRequired(true)
                  ->addValidator('NotEmpty');

        $lastName = new Zend_Form_Element_Text('lastName');
        $lastName->setLabel('Last name')
                 ->setRequired(true)
                 ->addValidator('NotEmpty');

here we create an Email Field and set its label to Email Address.
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email address')
the addFilter method convert all the string of the field to lowercase;
              ->addFilter('StringToLower')
setRequired method make sure the field is required
              ->setRequired(true)
addValidator valid the fields to not be empty and has to be like email address.
              ->addValidator('NotEmpty', true)
              ->addValidator('EmailAddress'); 
              
     than we, as usual add a submit button and set its Label to Contact Us.   
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Contact us');
        
We have created a form now we will give all the elements to the $form object.
        $form->addElements(array($firstName, 
            $lastName, $email, $submit));

No comments:

Post a Comment