Sterc / FormIt

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

":" within a custom validator parameter causes problems #252

Open patrickatwsrn opened 3 years ago

patrickatwsrn commented 3 years ago

Bug report

Summary

This works:

datetime:required:isDate=^%Y-%m-%d %H:%i^

This doesn't work: datetime:required:isValidDate=^Y-m-d H:i^

The difference is that isDate() is a native formit validator and isValidDate is a custom validator.

Step to reproduce

This is my custom Validator:

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

    $value   = trim($value);     // The value of the field that was POSTed.
    $param = trim($param)   // For some reason I always get a blank space at the end of the param string
    // empty dates should be filtered by :required 
    if(empty($value))  return true;

    // Standard Format for date validation
    if(empty($format)) $format = 'Y-m-d';

    $isValidDate = isValidDate($value,$param);

    if($isValidDate === true){
        return true;
    }
    else {
        if(!empty($key)){
            $validator->addError(
                 $key
                ,'String must have this format: "' . $param . '"'
            );
        }
        return false;
    }    

Observed behavior

And this is what I get as an error message:

grafik

For some reason "Y-m-d H:i" turns into "Y-m-d H".

I suspect that the validator function splits each validation line by ":" and then processes each part individually.

If there is a ":" inside the param part of a validator, the validator gets only part of the original string.

Expected behavior

This might be tricky. I guess there is an explode(':', $string) somewhere at work. It would have to ignore ":" inside he param part (^.H:i^).

Environment

I guess this problem is not related to server, php , or a software version.