Zend Framework 2: Empty isValid / getMessages

I create a registration form:

here is my action:

public function registrationAction() { $form = new RegistrationForm(); $request = $this->getRequest(); if ($request->isPost()) { $users = new Users(); $form->setInputFilter($users->getInputFilter()); $form->setData($request->getPost()); var_dump($form->isValid()); exit; if ($form->isValid()) { $users->exchangeArray($form->getData()); $this->getUsersTable()->addUser($users); $message = '  !             .'; } } return new ViewModel(array( 'form' => $form, 'message' => $message, )); } 

And here is my form:

 namespace Main\Form; use Zend\Form\Form; class RegistrationForm extends Form { public function __construct($name = null) { parent::__construct('User'); $this->add(array( 'name' => 'username', 'attributes' => array( 'type' => 'Zend\Form\Element\Text', ), 'options' => array( 'label' => ' ', ), )); $this->add(array( 'name' => 'email', 'attributes' => array( 'type' => 'Zend\Form\Element\Text', ), 'options' => array( 'label' => 'Email ', ), )); $this->add(array( 'name' => 'password', 'attributes' => array( 'type' => 'Zend\Form\Element\Text', ), 'options' => array( 'label' => '', ), )); $this->add(array( 'name' => 'submit', 'attributes' => array( 'type' => 'Zend\Form\Element\Submit', ), 'options' => array( 'label' => '', ) )); } 

}

Here is the inputFilter element in the Users model in Object \ Model \ Users.php:

 namespace Object\Model; use Zend\Validator\StringLength; use Zend\Validator\NotEmpty; use Zend\Validator\EmailAddress; use Zend\Validator\Date; use Zend\InputFilter\Factory as InputFactory; use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilterAwareInterface; use Zend\InputFilter\InputFilterInterface; class Users { public $user_id; public $username; public $name; public $email; public $password; public $sex; public $birthday; public $avatar; public $user_level; public $date_registered; public $is_active; public $is_banned; public function exchangeArray($data) { $this->user_id = (isset($data['user_id'])) ? $data['user_id'] : null; $this->username = (isset($data['username'])) ? $data['username'] : null; $this->name = (isset($data['name'])) ? $data['name'] : null; $this->email = (isset($data['email'])) ? $data['email'] : null; $this->password = (isset($data['password'])) ? $data['password'] : null; $this->sex = (isset($data['sex'])) ? $data['sex'] : null; $this->birthday = (isset($data['birthday'])) ? $data['birthday'] : null; $this->avatar = (isset($data['avatar'])) ? $data['avatar'] : null; $this->user_level = (isset($data['user_level'])) ? $data['user_level'] : null; $this->date_registered = (isset($data['date_registered'])) ? $data['date_registered'] : null; $this->is_active = (isset($data['is_active'])) ? $data['is_active'] : null; $this->is_banned = (isset($data['is_banned'])) ? $data['is_banned'] : null; } public function setInputFilter(InputFilterInterface $inputFilter) { throw new \Exception('Not used'); } public function getInputFilter() { if (!$this->inputFilter) { $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter->add($factory->createInput(array( 'name' => 'user_id', 'filters' => array( array('name' => 'Int'), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'username', 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'NotEmpty', 'options' => array( 'messages' => array( NotEmpty::IS_EMPTY => '   !', ), ), ), array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 50, 'messages' => array( StringLength::TOO_SHORT => '      1 ', StringLength::TOO_LONG => '      50 ', ), ), ), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'name', 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'max' => 255, 'messages' => array( StringLength::TOO_LONG => '        255 ', ), ), ), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'email', 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'NotEmpty', 'options' => array( 'messages' => array( NotEmpty::IS_EMPTY => '  email !', ), ), ), array( 'name' => 'EmailAddress', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 50, 'messages' => array( EmailAddress::INVALID => 'Email   .', ), ), ), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'password', 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'NotEmpty', 'options' => array( 'messages' => array( NotEmpty::IS_EMPTY => '  !', ), ), ), array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 4, 'max' => 90, 'messages' => array( StringLength::TOO_SHORT => '     4 ', StringLength::TOO_LONG => '     90 ', ), ), ), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'sex', 'filters' => array( array('name' => 'Int'), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'birthday', 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'Date', 'options' => array( Date::INVALID_DATE => '   .', ), ), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'avatar', 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'user_level', 'filters' => array( array('name' => 'Int'), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'date_registered', 'filters' => array( array('name' => 'Int'), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'is_active', 'filters' => array( array('name' => 'Int'), ), ))); $inputFilter->add($factory->createInput(array( 'name' => 'is_banned', 'filters' => array( array('name' => 'Int'), ), ))); $this->inputFilter = $inputFilter; } return $this->inputFilter; } 

}

When I try to access the isValid () or getMessages () methods of the $ form object, they are empty. When you call them in var_dump, they are always logical false ... Can someone help me? Thank you in advance. This only happens in this module. In my other module, everything is fine with the forms. This ZF2 is so complex at times ...: |

+4
source share
5 answers

I had the same problem as you, isValid () will return false, and getMessages () will be an empty array.

My problem was that I defined an input validator for the field (e.g. email), but that field did not exist in the form I was checking. There seems to be a bug in Zend Framework 2 that in this case does not print error messages: error report in github

So the solution is to add the missing field to the form object.

+9
source

Check that your field name entries match the name in the input filter - I did the same.

+6
source

I had the same problem, and that was because the form and filter did not have the same number of attributes.

Your registration form has fewer properties than your filter.

+2
source

Is it normal that you wrote that (in the controller)?

 var_dump($form->isValid()); exit; 

Maybe about "messages" are displayed only when an error occurs, not?

0
source

I could not find the error, but here are some tips:

You do not need to use InputFactory to set elements in InputFilter, ZF2 will do it automatically ( ZF2 Docs )

Finally, the default implementation of InputFilter is supported by Factory. This means that when you call add (), you can specify a specification that Factory will understand and create the corresponding object. This way you can create Input or InputFilter objects.

So instead

 $inputFilter->add($factory->createInput(array( 'name' => 'user_id', 'filters' => array( array('name' => 'Int'), ), ))); 

You can do

 $inputFilter->add(array( 'name' => 'user_id', 'filters' => array( array('name' => 'Int'), ), )); 

Another thing is if you call $ form-> isValid () two times for the same instance, the second causes some invisible error, and the rest of the code will not be executed. I do not know what is happening, but you can call isValid once for each instance of $ form.

0
source

All Articles