Zend2 Forms

Hi there,

I’m getting a little tied up with Zend forms. There seems to be a number of different ways to set up a form in zf2, and I can’t seem to get to the crux of what I’m trying to achieve.

I wan’t to set up a simple form with validation, so that when its processed I can check the isValid() method, and then handle the post data accordingly. Here’s what I’ve got so far:


<?php
namespace MyForm\\Form;

use Zend\\Form\\Form;

class MyForm extends Form{

	public function __construct($name = null){
		parent::__construct('MyForm');
		$this->setAttribute('method', 'post');
		$this->add(
			array(
				'name' => 'id',
				'type' => 'Hidden'
                                'validators' => array(
                                    'required' => true
                                )
			)
		);
		$this->add(
			array(
				'name' => 'name',
				'type' => 'Text',
				'attributes' => array(
					'placeholder' => 'Name'
				),
                                'validators' => array(
                                    'required' => true
                                )
			)
		);
		$this->add(
			array(
				'name' => 'email',
				'type' => 'Email',
				'attributes' => array(
					'placeholder' => 'Email'
				),
                                'validators' => array(
                                    'required' => true
                                )
			)
		);
		$this->add(
			array(
				'name' => 'submit',
				'type' => 'Submit',
				'attributes' => array(
					'value' => 'Go'
				)
			)
		);
	}
}

…and the controller


namespace MyForm\\Controller;

use Zend\\Mvc\\Controller\\AbstractActionController;
use Zend\\View\\Model\\ViewModel;
use Application\\Model\\Application;
use MyForm\\Form\\MyForm;

class MyFormController extends AbstractActionController
{
    public function indexAction()
    {
    	$form = new MyForm($status);

    	$request = $this->getRequest();
    	if($request->isPost()){

    		$form->setData($request->getPost());

       		if($form->isValid()){
       			echo "VALID";
       		} else {
       			echo "INVALID";
       		}
    	}

        return new ViewModel(array('form' => $form));
    }
}

…and finally the view


<h1>MyForm</h1>

<?php
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('name'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('submit'));
echo $this->form()->closeTag();
?>

Its rendering the form fine, but isValid() always returns false, and there are no error messages displayed on the form.

Any ideas anyone?

Many thanks in advance,
Mike

Hi Mike,

You’ve got a hidden field ‘id’ as part of your form, which is set as a required field, but I can’t see where the value of the field initially gets assigned?

It’s not.

Originally I wanted the form to fail. To test whether it passed, I just added a value to it client side.

Either way, there are no error messages displayed…

How are you getting the error messages to display? Are you calling $form->getMessages()?

Ah-ha!

Ok so $form->getMessages() returns an array of error messages, but in the Zend Getting Started guide (http://framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html), the error messages are printed on the form automatically.

I guess that has something to do with using the InputFilter method they use in the tutorial??

No I guess that’s because in the tutorial they use $this->formCollection($form); to output all the form elements, including errors etc. Check the explanation just before the ‘Editing an Album’ section.

Yep, I see…

So since I need to have a bit more control over the layout of my form, I’ll have to get the error messages manually, or I guess write a new class to handle that.

Thanks for your help fretburner.

So I just thought I’d update this thread a little after I’ve gained a little more understanding.

Firstly, you can’t add validators to the Form::add() method. I guess since it takes an array, it just ignores any keys that aren’t relevant to the method.

In order to add validators, you need to create a new class which extends Zend\InputFilter\InputFilter, and then add all the validators to that like so:


<?php
namespace MyForm\\Form;

use Zend\\InputFilter\\InputFilter;

class MyFormFilter extends InputFilter
{
	public function __construct()
	{
		$this->add(
			array(
				'name' => 'id',
				'required' => true,
			)
		);
		$this->add(
			array(
				'name' => 'name',
				'required' => true,
			)
		);
		$this->add(
			array(
				'name' => 'email',
				'required' => true,
			)
		);
	}
}

Then bind it to the form in the controller script:


namespace MyForm\\Controller; 

use Zend\\Mvc\\Controller\\AbstractActionController; 
use Zend\\View\\Model\\ViewModel; 
use Application\\Model\\Application; 
use MyForm\\Form\\MyForm;
use MyForm\\Form\\MyFormFilter //import the new class here

class MyFormController extends AbstractActionController 
{ 
    public function indexAction() 
    { 
        $form = new MyForm($status); 

        $request = $this->getRequest(); 
        if($request->isPost()){ 
            $form->setInputFilter(new MyFormFilter()); //bind the filter here
            $form->setData($request->getPost()); 

               if($form->isValid()){ 
                   echo "VALID"; 
               } else { 
                   echo "INVALID"; 
               } 
        } 

        return new ViewModel(array('form' => $form)); 
    } 
}

As for outputting the error messages, in the view file, the getRow() method, will render any error message, as long as you prepare the form first:


<h1>MyForm</h1> 

<?php 
$form->prepare(); //prepare the form here
echo $this->form()->openTag($form); 
echo $this->formHidden($form->get('id')); 
echo $this->formRow($form->get('name')); 
echo $this->formRow($form->get('email')); 
echo $this->formRow($form->get('submit')); 
echo $this->form()->closeTag(); 
?>

I hope that helps anyone in the same boat.

M