Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with isDate validator #249

Open
patrickatwsrn opened this issue May 22, 2021 · 1 comment
Open

Problems with isDate validator #249

patrickatwsrn opened this issue May 22, 2021 · 1 comment

Comments

@patrickatwsrn
Copy link

patrickatwsrn commented May 22, 2021

Bug report

isDate validator is returning true for some dates that are not valid.

Summary

core/components/formit/src/FormIt/Validator.php, line #658

    public function isDate($key, $value, $format = '%m/%d/%Y')
    {
        /* allow empty isDate, :required should be used to prevent blank field */
        if (empty($value)) {
            return true;
        }
        $ts = strtotime($value);
        if ($ts === false) {
            return $this->_getErrorMessage($key,'vTextIsDate','formit.not_date',array(
                'format' => $format,
                'field' => $key,
                'value' => $value,
            ));
        }
        if (!empty($format)) {
            $this->fields[$key] = strftime($format,$ts);
        }
        return true;
    }

strtotime is not a sufficient way to validate a date string because it is "fixing" problems it should not.

Step to reproduce

Simple snippet to test if there is a February 29th in a given year:

<?php 
# Name:  testDate snippet
# Usage: [[!testDate? &input=`2020-02-29`]]

if(strtotime($input)) {
   return date('Y-m-d',strtotime($input));
}
else {
   return "false";
}

2020 was a leap year.

  • Input: 2020-02-29
  • Output: 2020-02-29
  • Error: No

2021 was NOT a leap year

  • Input: 2021-02-29
  • Output: 2021-03-03
  • Error: Yes !!!

Every day <= 31 returns for february a day in the next month

  • Input: 2021-02-31
  • Output: 2021-03-01 !!!
  • Error: Yes

It does the right thing if it encounters impossible dates:

  • Input: 2020-13-01
  • Output: false
  • Error: No

Observed behavior

Validation of dates within the bounderies of february is not trustworthy.

Expected behavior

Dates within the scope of february should be handled in a proper way.

The php function checkdate() might be helpful here.

Docs: https://www.php.net/manual/en/function.checkdate.php

Environment

MODX 2.8.1, Formit 4.2.6 , PHP 7.4.18, Apache Webserver Running on Ubuntu Linux.

@patrickatwsrn
Copy link
Author

Something like this shold do the trick:

    function isValidDate(string $date, string $format = 'Y-m-d'){
        // php 
        $dateObj = DateTime::createFromFormat($format, $date);
        return $dateObj && $dateObj->format($format) == $date;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant