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: match | Kirby CMS

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

mhm. I’m working on a page with ajax-communication and I have a [page].json.php-file that does that magic.

SO when testing the form-input I always get the date-field returned as incorrect.

This is what I send as date: 2021-03-31
(it’s actually formatted by the browsers input type=“date”-field)
And that is exactly, what I get back, together with the “invalid”-message.

Here is the code from the json.php-file:

  $data = [
      '...'
      'record_id'  => get('record_id'),
      'datum'  => get('datum'),
      '...'
  ];

  $rules = [
      '...'
      'record_id'  => ['required', 'integer'],
      'datum'  => ['required', 'date'],
      '...'
  ];

  $messages = [
      '...'
      'datum'  => 'Datumsformat inkorrekt: '.get('datum'),
      '...'
  ];
  if($invalid = invalid($data, $rules, $messages)) 
  {
      echo json_encode($invalid);
  }

Can I expect to have a date-string like that accepted by invalid()?
If not, why?

kind regards,
Ralf

Such a data string should actually validate, which you can test with

dump(V::date('2021-03-31'));

It does validate if I use it in plain page-template. But still not in my JSON-representation.
Tried switching date-formats

2021/03/31
31-03-2021
31/03/2021
31.03.2021

No difference …

Might be, there is a misbehaviour of invalid(), while v::date() works correctly?

I just tested both functions in the page-template:

<?= dump(V::date('2021-03-31')); ?>
<?= dump(invalid(["datum"=>"2021-03-30"],["datum"=>['date']],["datum"=>'Datum falsch'])); ?>

That is the result:
kirby

(why is the output doubled?)

So V::date checks ok while invalid() returns an error …

I think there is a bug in the validator, or in the way the helper passes the data to the validator.

Because there are two arguments passed, so the validator doesn’t go into the simple date validation, so the second part of this if statement

 if (is_int($value) !== true || is_int($test) !== true) {

fails.

:+1:

hard to believe a problem with a standard validation like that has not been detected earlier.
But I’m glad it will be fixed soon.