I'm having a form with an element username
. There are two validators: NotEmpty
and StringLength
. The custom error messages for StringLength
are working, but somehow it does not use the custom error message for the NotEmpty
validator. In ZF1 the notEmpty validator was added automatically when making an element required which could be turned off. I can't find such an option in ZF2 and maybe my NotEmpty validator is not used because it was already added by 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.',
)
),
),
),
)));
var_dump($form->getMessages())
to see what message key is actually used. Maybe the required message gets checked before the is_empty check and therefore you don't even reach the is_empty check. Check ifNotEmpty::IS_EMPTY
equals the message key from the output messages – Sam