Invalid() Kirby function, can we check date format?

Can we validate a date format with the invalid() function?

Something like:

$data = ['date' => $date];
$rules = ['date' => array( 'required' , 'date' => 'mm/dd/yyyy' )];
$messages = ['date' => array( 'A date is required', 'The date format must be mm/dd/yyyy.' )];
$invalid = invalid( $data, $rules, $messages);

It seems to works partially.

tretre
33/66/9999

Return false as expected

But

12/31/12
12/31

Return true

@gillesvauvarin have you tested if the v::date() method works as expected?

The v::date() method only checks for a valid date, not for a particular date format. E.g.if you do a dump

dump(date_parse('12/31')); // date_parse is use inside the v::date validator

…you get an array like this:

Array
(
    [year] => 
    [month] => 12
    [day] => 31
    [hour] => 
    [minute] => 
    [second] => 
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 
)

So I think you would need a custom validator to check the date format, rather than using the default date validator that just checks if the given date is a valid date in the sense of date_parse

Even the Kirby3 Date format 1999-06-25 is not valid? :slight_smile:

$data = array(
	'strasse'       => esc(get('strasse')),
	'plz'           => esc(get('plz')),
        'ort'           => esc(get('ort')),
        'mobile'        => esc(get('mobile')),
        'geburtstag'    => esc(get('geburtstag')),
);
$rules = array(
	'plz'           => array('min' => 1000, 'max' => 9999 ),
        'mobile'        => array('required', 'min' => 9),
        'geburtstag'    => array('required', 'date'), 

Or am I doing something wrong?

Doesn’t date need a format?

https://getkirby.com/docs/reference/system/validators/date

I just checked the Referenz…

Yeah, right. Hm…

Just tested and works for me, no error and message gets send.

I also tested a date format like 21.10.2019 and it validates as well.

You are right! It was another bug in the code! :grimacing:

Hello @texnixe, i would also like to validate a date format with the invalid() function?
I tried several solutions without success, and i am still not sure,
if it is possible to use the invalid-method to correctly validate dates from a form-input.
How did you format the german date in your example?

$rules = [
    'name' => ['required', 'min' => 3],
    'email' => ['required', 'email'],
    'birthday' => ['required', 'date' => 'dd.mm.yyyy']
];

if i dump the date, i don’t get any errors,
but the invalid alert contains an error.
image

name and email are working as expected.
Do you suggest a custom-validation for dates?

Tnx.

I don’t think you can validate the date format with V::date(), if you look at the source code, you’ll see that all the validator does is check if the given string can be parsed as a date: https://github.com/getkirby/kirby/blob/29b36fffa6527804ac0fdea757c77878c3ccd1b8/src/Toolkit/V.php#L250

So if you want to check a particular format, I’d check against a regex pattern. V::match should do the job for these cases: https://getkirby.com/docs/reference/system/validators/match

ok, to validate for the particular format ‘dd.mm.yyyy’ i changed the rule to:

'dob' => ['required', 'match' => '/^([0-9]{1,2})\\.([0-9]{1,2})\\.([0-9]{4})$/']

After the date-format is valid i will check if the date ist valid with php checkdate
tnx @texnixe