There are two errors in your configuration.
The first one is where you declare your validator options (the format), which must be just after declaring the validator's name.
The second one is the format you are using. Unless you don't want to represent dates as "Wed-Jan-2020" (today's date), the correct format is d-m-Y
.
According to PHP documentation, format parameters are:
| character | description | example |
|-----------|----------------------------------------------------------|------------------------|
| D | A textual representation of a day, three letters | Mon through Sun |
| M | A short textual representation of a month, three letters | Jan through Dec |
| d | Day of the month, 2 digits with leading zeros | 01 to 31 |
| m | Numeric representation of a month, with leading zeros | 01 through 12 |
| Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
The correct inputfilter declaration is:
$inputfilter = new \Zend\InputFilter\InputFilter();
$inputfilter->add([
'name' => 'geboortedatum',
'required' => true,
'filters' => [
['name' => \Zend\Filter\StringTrim::class]
],
'validators' => [
[
'name' => \Zend\Validator\Date::class,
'options' => [
'format' => 'd-m-Y'
]
]
]
]);
$dates = [
'1977-05-23',
'23-05-1977',
'30-02-2020'
];
foreach ($dates as $date) {
$data['geboortedatum'] = $date;
$inputfilter->setData($data);
echo 'Date "' . $data['geboortedatum'] . '" is ';
if ($inputfilter->isValid()) {
echo 'valid';
} else {
echo 'invalid. Errors:'
. PHP_EOL . "\t"
. implode($inputfilter->getMessages()['geboortedatum'], PHP_EOL . "\t");
}
echo PHP_EOL;
}
Result:
Date "1977-05-23" is invalid. Errors:
The input does not fit the date format 'd-m-Y'
The input does not appear to be a valid date
Date "23-05-1977" is valid
Date "30-02-2020" is invalid. Errors:
The input does not fit the date format 'd-m-Y'
The input does not appear to be a valid date