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

idDate Validator changes valid values after submit (solved!) #250

Open
patrickatwsrn opened this issue May 23, 2021 · 0 comments
Open

idDate Validator changes valid values after submit (solved!) #250

patrickatwsrn opened this issue May 23, 2021 · 0 comments

Comments

@patrickatwsrn
Copy link

patrickatwsrn commented May 23, 2021

Before you read this...

Never copy your old errors into a new project!

date:required:isDate==^%Y-%m-%d

The prefixing "=" was due to the extra "=" after :isDate. Instead of breaking the validation, it got added to the input value. Something similar happened because of a line break at the end of the validation rule.

Don't use linebreaks like I did:

Insterad of:

   &validate=`
    someDate:required:isDate=^%Y-%m-%d^
   ,name:required`

Use

   &validate=`
    someDate:required:isDate=^%Y-%m-%d^,
   name:required`

Don't put the comma BEFORE the next line, but put it AFTER you validation rule.

If you don't do that, the linebreak gets transfered into a magic empty space character that is added to the original value.

*** Question *** I might be wrong here, but this should not break the validation and therefore it would be great to fix it.

Bug report (obsolute)

Using

date:isDate==^%Y-%m-%d^

as a validation contrain results in a change of the original variable.

Summary

Quick summary what's this issue about.

Step to reproduce

Testcase: A simple form that requires input for the fields "date" and "name" and also validates "date" to be a valid Date.

[[!FormIt?
   &validate=`date:required:isDate==^%Y-%m-%d^,
      name:required`
]]

<form action="[[++site_url]][[~[[*id]]]]" method="post">
    <div>
        <label> Name<br>
            <input type="text" name="name" value="[[+fi.name]]" />
        </label>
        [[+fi.error.name]]
    </div>
    
    <div>
        <label> Date<br>
            <input type="text" name="date" value="[[+fi.date]]" />
        </label>
        [[+fi.error.date]]    
    </div>

    <hr>

    <button type="submit" name="submit" value="test">Send</button>
</form>

Observed behavior

This form expects name and date to be mandatory and date have this form YYYY-MM-DD

Example 1a: Enter values and hit submit

grafik

Enter a valid date "2020-12-12" but leave name empty.

Example 1b: Show the form again if the validation failed.

grafik

The form gets reloaded and the former values get inserted again.

As you can see a "=" is prefixing the date.

This happens ONLY if the validation was successful! If you enter an invalid date, the validator returns the original values.

Example 2a: Enter values and hit submit

grafik

2020-31-12 is not a valid date, because MM can only between 01 and 12.

Example 2b: Show the form again if the validation failed.

grafik

The validation for date failed. The value is returned, but it is not prefixed by a "="

Edit:

  • not only is a "=" prefixed to the return value, there is also an empty space character suffixed to it.
  • The total content would look like this: date = '=2021-05-21 ' and not '2021-05-21';

grafik

Where not to look for a solution

# core/components/formit/src/FormIt/Validator.php, Line # 652
    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;
    }

There is nothing here, that could explain this.

Expected behavior

The validtor should alway return the original value. The value should not be modified in any way.

Environment

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

@patrickatwsrn patrickatwsrn changed the title idDate Validator prefixes valid values with a "=" after submit idDate Validator changes valid values after submit May 25, 2021
@patrickatwsrn patrickatwsrn changed the title idDate Validator changes valid values after submit idDate Validator changes valid values after submit (solved!) May 25, 2021
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