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

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

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

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

patrickatwsrn opened this issue May 26, 2021 · 0 comments

Comments

@patrickatwsrn
Copy link

patrickatwsrn commented May 26, 2021

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.

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