Laminas\Validator\Date Handling for 2019-02-31

Hello,

I am laminas newbie. I dont find an error constants for this example. How can I handle this?

php > $dtZone = new DateTimeZone(‘UTC’);
php > $dtString = ‘2019-02-31’;
php > $dt = new DateTime($dtString, $dtZone);
php > echo $dt->format(‘Y-m-d’);
2019-03-03

This example is an input to Form\Element\DateSelect. for this request only!

Andy

Hello and welcome to forums! :smiley:

Unfortunately I don’t know what your goal is.

The form element Laminas\Form\Element\DateSelect uses the Date validator from laminas-validator. There will you find all supported constants:

Maybe you can describe exactly what you have in mind then we can better help.

Hello. :slight_smile:

My english is terrible! Sorry about.

I builded a Form\Form and use inside Form\Element\DateSelect.

I see (in html ) Day-Select(01-31), Month-Select(01-12) and Year-Select(configure with max_year and min_year),
It works.
Unfortunately you can select 31.02.2019 and I see in database 2019-03-03.

I wanto to use in InputFilter:
‘options’ => [
‘messages’ => [
Validator\Date::ANY_CONST => ‘message’
]
}

Greetings

Please check which date value the form returns after validation:

if ($form->isValid()) {
   var_dump($form->getData());
}

I forgot to activate validator. sorry!
But now i get ( after $form->isValid() ):
Array
(
[username] => test
[gender] => Male
[email] => test@example.de
[confirm_email] => test@example.de
[birthday] => NULL
[password] => test12345
[confirm_password] => test12346
[csrf] => dc90606f4a97f0d10ea0c030f83dd3d2-5555be22ef186525e50704a74e6e33b5
[create_account] => Account anlegen
)

Validator\NotEmpty should fire an error for birthday or? :frowning:

[

        'name' => 'birthday',

        'required' => true,

        'filters' => [

            [ 'name' => Filter\StripTags::class ]  , # remove html tags

            [ 'name' => Filter\StringTrim::class ] , # remove empty spaces               

        ],

        'validators' => [

            [ 

                'name' => Validator\NotEmpty::class,

                'options' => [

                    'messages' => [

                        Validator\NotEmpty::INVALID => 'invalid',

                        Validator\NotEmpty::IS_EMPTY => 'Empty',

                    ]

                ]

            ],

            [

                'name' => Validator\Date::class,

                'options' => [

                    'format' => 'Y-m-d',

                    'messages' => [

                        Validator\Date::FALSEFORMAT => 'Birthday has wrong format',

                        Validator\Date::INVALID_DATE => 'Date ist not valid',

                        Validator\Date::INVALID => 'Invalid'

                    ],

                ],

            ],

        ],

    ]

I inserted, what I found.

Here a short example to demonstrate different values for validation:

class ExampleForm extends Laminas\Form\Form
    implements Laminas\InputFilter\InputFilterProviderInterface
{
    public function init() : void
    {
        $this->add(
            [
                'name' => 'birthday',
                'type' => Laminas\Form\Element\DateSelect::class,
            ]
        );
    }

    public function getInputFilterSpecification() : array
    {
        return [
            [
                'name'       => 'birthday',
                'required'   => true,
            ],
        ];
    }
}

Validation with invalid value:

$form = new ExampleForm();
$form->init();

$form->setData(
    [
        'birthday' => '',
    ]
);
var_dump($form->isValid()); // false
var_dump($form->getMessages()['birthday']['isEmpty']); // "Value is required and can't be empty"

With valid values:

$form = new ExampleForm();
$form->init();

$form->setData(
    [
        'birthday' => '2020-07-01',
    ]
);
var_dump($form->isValid()); // true
$form = new ExampleForm();
$form->init();

$form->setData(
    [
        'birthday' => [
            'year'  => '2020',
            'month' => '07',
            'day'   => '01',
        ],
    ]
);
var_dump($form->isValid()); // true

Maybe it will help to find your problem.

Thanks for the example :slight_smile: