Hiho, I would like to validate a date field from an zf2 form. I set the 'format' option to get the format I need. But at every time I validate it i get an error. The validator looks like this:
$inputFilter->add($factory->createInput(array(
'name' => 'user_data_birth',
'required' => false,
'validators' => array(
array(
'name' => 'Date',
'options' => array(
'format' => 'd.m.Y',
'locale' => 'de',
'messages' => array(
\Zend\Validator\Date::INVALID => 'Das scheint kein gültiges Datum zu sein.',
\Zend\Validator\Date::INVALID_DATE => 'Das scheint kein gültiges Datum zu sein. (Invalid Date)',
\Zend\Validator\Date::FALSEFORMAT => 'Das Datum ist nicht im richtigen Format.',
),
),
),
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Bitte geben Sie das Datum an'
),
),
)
),
)));
But I get every time an error that the date is in the wrong format.
Did you try with another format like the basic: 'Y-m-d' ? What is the result?
You can solve the problem of, start date should be less than end date validation, using the Callback function as below:
Using the above code, I have solved my problem. I hope this helps.
CallBack function is a very convenient way if we need to use this only in one form. Most times we will need to use this in more than one place. I did some research and wrote this custom validator. This validator compares two strings; I have added another trick to see if we need these strings to be different or same.
If we are changing the password; then the old password and the new password to be different at the same time we need the new password verification to be the same as the new password. This validator can be used be used for both cases just by changing the different parameter to be "true" or "false".
create a new validator StringCompare.php in Application\src\Application\Validator
Add the following to the Form filter
Perform the form validation in your controller and we are through. If we use different = 'true' the validation routine ensures the two values are different, if we use 'false', then it ensures that the strings are the same.
need to change validator which is in date element. If you pass format into the element you will be fine. You can take validator from element too.
or