Zend Framework 2: NotEmpty Validator Post

I have a form with username element. There are two validators: NotEmpty and StringLength . Custom error messages for StringLength work, but somehow it doesn't use a custom error message for NotEmpty validator. In ZF1, the notEmpty validator was added automatically when creating the required element, which could be disabled. I can not find such an option in ZF2, and maybe my NotEmpty validator is not used, because it is already added with the required flag !?

  $inputFilter->add($factory->createInput(array( 'name' => 'username', 'required' => true, 'filters' => array( array( 'name' => 'StringTrim' ), ), 'validators' => array( array( 'name' => 'NotEmpty', 'options' => array( 'messages' => array( NotEmpty::IS_EMPTY => 'Bitte geben Sie Ihren Benutzernamen ein.', ), ), ), array( 'name' => 'StringLength', 'options' => array( 'min' => 3, 'max' => 45, 'messages' => array( StringLength::TOO_SHORT => 'Der Benutzername muss mindestens 3 Zeichene lang sein.', StringLength::TOO_LONG => 'Der Benutzername darf maximal 45 Zeichen lang sein.', ) ), ), ), ))); 
+6
source share
2 answers

It was a bug in the version I used. After upgrading to the latest version of GitHub, it works great.

+2
source

The ZF2 form by default adds validators for various form elements automatically when they are created. To prevent the addition of default values ​​and the presence of only your validators, you must add to the form constructor:

$ this-> setUseInputFilterDefaults (false);

This will prevent the Form object from adding these default validators. Now, when you add your own NotEmpty or some other element, a validator with a custom message, this message is used instead of the standard one.

+5
source

Source: https://habr.com/ru/post/923112/


All Articles