ZendFramework 2.0.0rc3 form

I try to validate with the form but I cant get error messages.

//this is my code:

$form = new TestForm();
$form->setInputFilter(new TestFilter());
$data = array('id'=>'','email'=>'myemail@myemail.com');
$form->setData($data);

if($form->isValid()){
    echo 'ok';
} else {
echo 'not ok <br/>';
$messagesForm = $form->getMessages();
$filter=$form->getInputFilter();
$messagesFilter=$filter->getMessages();
var_dump($messagesForm);
var_dump($messagesFilter);
}

/////////////////
Output
not ok
//messagesForm

    array
     empty
//MessagesFilter
    array
    'id' => 
    array
      'isEmpty' => string 'Value is required and can't be empty' (length=36)

_

How is possible? The filter is ok, but I can't get error messages from the form Could be a bug or I made something wrong?

FULL code:

TestFilter: _

<?php
namespace mvc\filter;  
use Zend\InputFilter\InputFilter;  
use Zend\InputFilter\Factory as InputFactory;  

class TestFilter extends InputFilter  
{  
        public function __construct()  
        {  
            $factory = new InputFactory();  
            $this->add($factory->createInput(array('name'=>'id','required'=>true)));  
            $this->add($factory->createInput(array('name'=>'email','required'=>true)));  
        }  
}  
?>

_

TestForm _ namespace mvc\form; use Zend\InputFilter\Factory; use Zend\Form\Element; use Zend\Form\Form;

class TestForm extends Form  
{  
public function prepareElements()  
{  
    $this->add(array(  
        'name' => 'id',  
        'attributes' => array(  
            'type'  => 'text',  
            'label' => 'Your name',  
        ),  
    ));  

        $this->add(array(  
        'name' => 'email',  
        'attributes' => array(  
            'type'  => 'email',  
            'label' => 'Your email address',  
            ),  
        ));  
    }  
}  
?>

_

Answers


I have not worked with ZF2 yet but try to add validators to your elements:

use  Zend\Validator;
$this->add(array(  
    'name' => 'id',  
    'attributes' => array(  
        'type'  => 'text',  
        'label' => 'Your name',  
    ),
    'validator' => new StringLength(array('max'=>20))
));  
}  

you must specify 'validators', example of a getInputFilter:

use
Zend\InputFilter\InputFilter,
Zend\InputFilter\Factory as InputFactory,
Zend\InputFilter\InputFilterAwareInterface,
Zend\InputFilter\InputFilterInterface;

class User implements InputFilterAwareInterface
{
public function getInputFilter()
{
    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();

        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
            'name'     => 'email',
            'required' => true,
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min'      => 1,
                        'max'      => 100,
                    ),
                ),
            ),
        )));
      $this->inputFilter = $inputFilter;        
    }

    return $this->inputFilter;
}

You may do it this way by retrieving input filter from your model and then setting it to a form:

...
$form = new ItemForm();
$form->setInputFilter($user->getInputFilter());
$form->setData($params);
if ($form->isValid()) {
  ...
}
...

Need Your Help

Sharepoint 2013 - Fetch only last approved version through REST api

api rest sharepoint sharepoint-2010

Before I ask my question I want to give you a quick overview about the topic.

Web application Development - Subdomain

subdomain

I'm currently at the very early design stage of building a web app that will be used by companies. Each company will have many departments and each department many staff. Each department will manag...