Simple Form Validator For Zend Framework 2 Forms

October 1, 2012

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

Step 1.

Extending Zend Framework 2 Form.

__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;

 

Suggestions or problems ?

Write a comment.

tags: , ,
posted in Zend Framework 2 by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org