Sterc / FormIt

A dynamic form processing Snippet for MODX Revolution
https://docs.modx.com/current/en/extras/formit
33 stars 58 forks source link

islowercase validator not working (solved) #251

Open patrickatwsrn opened 3 years ago

patrickatwsrn commented 3 years ago

Before you read this...

The problem was caused because I have a habit of putting commas in front of the following line:

[[!FormIt?
   &validate=`
         lowercasetext:required:islowercase
         ,name:required`
]]

This breaks the validation.

[[!FormIt?
   &validate=`
         lowercasetext:required:islowercase,
         name:required`
]]

This works.

In a similar bug report reported by me the issue was caused by the same problem.

Bug report

Summary

islowercase always seems to return true, regardless of validated content.

Step to reproduce

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

[[!FormIt?
   &validate=`
         lowercasetext:required:islowercase
        ,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>islowercase<br>
            <input type="text" name="lowercasetext" value="[[!+fi.lowercasetext]]" />
        </label>
        [[!+fi.error.lowercasetext]]
    </div>

    <hr>

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

Observed behavior

Insert string "lower"

grafik

After submiting the form:

grafik

Insert string "LOWER"

grafik

After submiting the form:

grafik

It doesn't matter what goes into the lowercasetext. The validator never returns an error. BAD

Possible Problems

This does not seem to be related to the lexicon file, because there is a string for this error:

Ths issue should be located in validator.php, but to be honest I can't see what could ever go wrong here:

core/components/formit/src/FormIt/Validator.php, LINE # 678

    /**
     * Checks to see if a string is all lowercase
     * @param string $key The name of the field
     * @param string $value The value of the field
     * @return boolean
     */
    public function islowercase($key, $value)
    {
        $v = $this->config['use_multibyte'] ? mb_strtolower($value,$this->config['encoding']) : strtolower($value);
        return strcmp($v, $value) == 0 ? true : $this->_getErrorMessage($key,'vTextIsLowerCase','formit.not_lowercase',array(
            'field' => $key,
            'value' => $value,
        ));
    }

According to my phpinfo page "Multibyte decoding support using mbstring" is enabled and the setting for "use_multibyte" is true. Comparing the original value with the strtolower or mb_strtolower.

Question: What is $this->config['encoding'] supposed to return? It is not a setting that can be displayed on the settings page. Neither core nor formit have this variable.

Expected behavior

"LOWER" should return an error. BAD "lower" should not return an error. FINE

Environment

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