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