March 16, 2014

March 16, 2014
In this article, we are going to discuss about How to implement the form validator in the Zend Framework 2 forms. Zend Framework is based on simplicity, object-oriented best practices, corporate friendly licensing, and a rigorously tested agile codebase.

Zend Framework is focused on building more secure, reliable, and modern Web 2.0 applications & web services, and consuming widely available APIs from leading vendors like Google, Amazon, Yahoo!, Flickr, as well as API providers and cataloguers like StrikeIron and Programmable Web.

Here is some extended core helping to auto validate Zend Framework 2 Forms.

Step 1 : Extending Zend Framework 2 Form.

<?php
//File : App_folder/module/Module_name/src/Module_name/Form/ExtendedForm.php
namespace Application\Form;
use Zend\Form\Form;

class ExtendedForm extends Form {
    protected $_name;
    protected $_rawElements = array();
    public function __construct($name = null) {
        parent::__construct($name);

    }
    public function isValid($request = null) {
        $this->__addValidator();
        if ($request -> isPost()) {
            $query = $request -> getQuery();
            $query = is_object($query) ? $query->toArray() : $query;
            $post = $request -> getPost();
            foreach($post as $var=>$value){
                $query[$var] = $value;
            }
            $this -> setData($query);
            return parent::isValid();
        } else {
            return false;
        }
    }
    public function add($elementOrFieldset, array $flags = array()) {
        $form = parent::add($elementOrFieldset, $flags);
        $this->_rawElements[] = $elementOrFieldset;
        return $form;
    }
    private function __addValidator() {
        $this -> setInputFilter(new ExtendedFormValidator($this->_rawElements));
    }

}

Step 2 : Creating Zend Framework 2 Form validator

//File : App_folder/module/Module_name/src/Module_name/Form/ExtendedFormValidator.php
namespace Application\Form;
use Zend\InputFilter\InputFilter;

class ExtendedFormValidator extends InputFilter {

    public function __construct($elements) {
        foreach ($elements as $element) {
            if (is_array($element)) {
                if (isset($element['type'])) {
                    unset($element['type']);
                }
                $this -> add($element);
            }
        }
    }

}

Step 3 : Creating simple Zend Framework 2 Form and extending it with ExtendedForm

//File : App_folder/module/Module_name/src/Module_name/Form/ResendPassword.php
namespace Application\Form;

class ResendPassword extends ExtendedForm
{
    public function __construct($name = null)
    {

        parent::__construct('login');
        $this->setAttribute('method', 'post');

        $this->add(array(
            'required'=>true,
            'name' => 'usermail',
            'type'  => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => 'Email',
            ),
            'filters'=>array(
                array('name'=>'StripTags'),
                array('name'=>'StringTrim'),
            ),
            'validators'=>array(
                array('name'=>'EmailAddress')
            ),

        ));

        $this->add(array(
            'name' => 'submit',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Submit',
                'id' => 'submitbutton',
            ),
        ));
    }
}

Step 4 : Instantiating the Zend Framework 2 Form.

//File : App_folder/module/Module_name/src/Module_name/Controller/IndexController.php
use Application\Form as Form; //at the top of the file.

public function forgotAction(){

    $form = new Form\ResendPassword();
    if($form->isValid($this->getRequest())){
          //do your magic
    }
    return new ViewModel(array('form'=>$form));  
}

Step 5 : Rendering Zend Framework 2 Form in the View.

//File : App_folder/module/Module_name/View/Module_name/index/index.phtml
$form =  $this->form;
$form->prepare();

echo $this->view->form()->openTag($form) . PHP_EOL;
$elements = $form->getElements();
foreach($elements as $element){
    echo $this->view->formRow($element) . PHP_EOL;
}
echo $this->view->form()->closeTag($form) . PHP_EOL;

0 comments:

Post a Comment