Invalid DateTime Keep Coming

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.

What is this? Iā€™m in 2024 or Iā€™m in 2400 or Iā€™m in 1990. I donā€™t know.

Thanks!

This is the validator that is triggering. Its attached to the DateTimeSelect element via its getValidator method iirc.

$dateTime = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', '2010-01-01T00:00Z');
var_dump($dateTime); // false

But:

$dateTime = DateTimeImmutable::createFromFormat('Y-m-d\TH:i', '2010-01-01T00:00');
var_dump($dateTime); // object(DateTimeImmutable) ā€¦

Not clear, please elaborate, is there a way to void this validation.

Please compare carefully the two examples. :slightly_smiling_face:

The value that reaches Laminas Validator is -
ā€œ2024-05-17T21:00ā€, so I should get object but that is not the case , I. am getting validation failed error.

Why is there a ā€œzā€ at the end of your string?

That is by mistake, I removed that and even then the error is there.

I do not understand the custom validator. Please check the following example without your custom validator:

$form = new Laminas\Form\Form();
$form->add(
    [
        'type'    => Laminas\Form\Element\DateTimeSelect::class,
        'name'    => 'my-date-field',
        'options' => [
            'label'    => 'Event Date',
            'format'   => 'Y-m-d\TH:i',
            'max_year' => 2020,
            'min_year' => 2010,
        ],
    ]
);
$form->getInputFilter()->add(
    [
        'name'     => 'my-date-field',
        'required' => true,
    ]
);

$form->setData(
    [
        'my-date-field' => [
            'year'   => '2012',
            'month'  => '02',
            'day'    => '29',
            'hour'   => '12',
            'minute' => '00',
        ],
    ]
);
var_dump($form->isValid()); // true

$form->setData(
    [
        'my-date-field' => [
            'year'   => '1990',
            'month'  => '02',
            'day'    => '29',
            'hour'   => '12',
            'minute' => '00',
        ],
    ]
);
var_dump($form->isValid()); // false

$form->setData(
    [
        'your-date-field' => [],
    ]
);
var_dump($form->isValid()); // false

Is this not enough?


See also the documentation for the correct options for setting the minimum and maximum years: MonthSelect - laminas-form - Laminas Docs
(the DateTimeSelect element extends the DateSelect element and this extends the MonthSelect element)

1 Like

Another question I have and maybe its just because I am tired and overlooking something (likely the case) is why you are using

As well as:

They even use different field names. One is ā€˜my-date-fieldā€™ the other is ā€˜pos-event-dateā€™. Iā€™m not sure what purpose this is supposed to serve. Is ā€˜pos-event-dateā€™ in a fieldset?

Actually, while copy/pasting the code from the real implementation to this post I forgot changing at one location. In real code they are same.My apologies for confusion.

Personally Iā€™ve only used getInputFilterSpecification() when working in fieldset classes. It and the $inputFilter->add call do similar things. Just in different ways (if memory serves). As @froschdesign mentioned you should use the default validator. Its already attached to the element it only needs to be properly configured.

@sanjivsharmalv

This is still wrong and completely unnecessary!

The related view helper FormLabel will do this:

If you have a Laminas\I18n\Translator\Translator attached, FormLabel will translate the label contents when rendering.

The topic has already been discussed: How is translation of labels achieved in Forms?

1 Like