Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

A not match regex validation option #18

Closed
robjbrain opened this issue Mar 18, 2016 · 9 comments
Closed

A not match regex validation option #18

robjbrain opened this issue Mar 18, 2016 · 9 comments

Comments

@robjbrain
Copy link

Laravel has the regex option which passes when the value matches a supplied regex.

Could we get an opposite method (similar to in & not_in) that fails if the value matches a supplied regex.

From what I can tell only two changes are needed:

Added to Validation/Validator.php

    protected function validateNotRegex($attribute, $value, $parameters)
    {
        if (! is_string($value) && ! is_numeric($value)) {
            return false;
        }

        $this->requireParameterCount(1, $parameters, 'regex');

        return !preg_match($parameters[0], $value);
    }

Added to resources/lang/en/validation.php

"not_regex"                => "The :attribute format is invalid.",

However i've never submitted a contribution before so wanted to start a discussion first, incase there were any complaints. There may also be better naming conventions than "not_regex". That's just what I used for my example.

@vlakoff
Copy link

vlakoff commented Mar 20, 2016

Considering there is no way to write a "negative regex" and I have lacked this on several occasions, I'd vote for this proposal. Just for example, could be used for forbidden usernames, too weak passwords, forbidden email hosts, etc.

A few refinements to your code:

protected function validateNotRegex($attribute, $value, $parameters)
{
    // ...

    $this->requireParameterCount(1, $parameters, 'not_regex'); // 'not_regex'

    return ! preg_match($parameters[0], $value); // space after "!"
}

Meanwhile, here is how to hook custom rules into the validator:

class CustomValidator extends \Illuminate\Validation\Validator;
{
    // ...
}

app('validator')->resolver(function () {
    return new CustomValidator(app('translator'), ...func_get_args());
});

@vlakoff
Copy link

vlakoff commented Mar 20, 2016

Ahem, I forgot Validator::extend...

Validator::extend('not_regex', function ($attribute, $value, $parameters, $validator) {

    if (! is_string($value) && ! is_numeric($value)) {
        return false;
    }

    // mmm, can't use requireParameterCount as it's protected
    //$validator->requireParameterCount(1, $parameters, 'not_regex');

    return ! preg_match($parameters[0], $value);
});

@robjbrain
Copy link
Author

Seems like you've picked up a bug in Validator::extend if it can't properly use requireParameterCount

Otherwise appreciate the response and amendments.

@franzliedke
Copy link

Hmm, could we use the Closure::bindTo() function to solve this problem? Pretty sure that makes sense for extensions...

That way, the extension closure would have access to the validator object as $this and - if I'm not mistaken - automatically access private and protected methods.

@vlakoff
Copy link

vlakoff commented Mar 26, 2016

I thought about this, but not sure it would be a good idea. Maybe people are using the current $this to access the class in which Validator::extend is called.

$validator is already passed to the callback, and it should be sufficient.

Nevertheless, it would be a breaking change.

@franzliedke
Copy link

Yes, that would be breaking backwards compatibility, but that would be okay in a major release.

I'd also argue that using the current $this is highly unlikely in a custom validator, since they're usually defined in a service provider. Even if I'm wrong (examples, please!), we'd still have the use clause.

@vlakoff
Copy link

vlakoff commented Mar 27, 2016

Refs laravel/docs#1833.

Though, to continue discussing about this requireParameterCount / $this stuff I think it would be better to open a new issue.

@taylorotwell
Copy link
Member

I'm pretty much OK with this general idea.

@vlakoff
Copy link

vlakoff commented Mar 10, 2018

PR submitted: laravel/framework#23475.

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

No branches or pull requests

5 participants