This is how I add element to form in Laminas:
// Add "my-date-field" field
$this->add([
'type' => DateTimeSelect::class,
'name' => 'my-date-field',
'options' => [
'label' => $translator->translate('Event Date'),
'format' => 'Y-m-d\TH:i',
'attributes' => [
'min' => '2010-01-01T00:00Z',
'max' => '2020-01-01T00:00Z',
'step' => '1', // minutes; default step interval is 1 min
],
],
]);
This is how add to input filter:
$inputFilter->add([
'name' => 'my-date-field',
'required' => true,
'validators' => [
[
'name' => MyDateValidator::class,
'options' => [
'format' => 'Y-m-d\TH:i',
],
],
],
]);
This is MyDateValidator:
public function isValid($value)
{
$this->setValue($value);
// Custom validation logic
$format = 'Y-m-d\TH:i'; // Define your expected date format
$date = \DateTime::createFromFormat($format, $value);
if ($date && $date->format($format) === $value) {
return true;
}
$this->error(self::INVALID_DATE);
return false;
}
But I keep getting ādateInvalidDateā error message on form->valid():
{
["'my-date-field"]=>
array(1) {
["dateInvalidDate"]=>
string(44) "The input does not appear to be a valid date"
}
}
The ādateInvalidDateā is thrown by Laminas where as my error array index is āinvalidDateā. I do not get āinvalidDateā but I always get ādateInvalidDateā . My question is how do I stop Laminas from throwing ādateInvalidDateā. I tried:
public function getInputFilterSpecification()
{
return [
'pos-event-date' => [
'required' => false,
'allow_empty' => true,
'disable_inarray_validator' => true, // Disable any default in-array validator
],
];
}
Still of no use.