-
Notifications
You must be signed in to change notification settings - Fork 28
A not match regex validation option #18
Comments
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());
}); |
Ahem, I forgot 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);
}); |
Seems like you've picked up a bug in Validator::extend if it can't properly use requireParameterCount Otherwise appreciate the response and amendments. |
Hmm, could we use the That way, the extension closure would have access to the validator object as |
I thought about this, but not sure it would be a good idea. Maybe people are using the current
Nevertheless, it would be a breaking change. |
Yes, that would be breaking backwards compatibility, but that would be okay in a major release. I'd also argue that using the current |
Refs laravel/docs#1833. Though, to continue discussing about this |
I'm pretty much OK with this general idea. |
PR submitted: laravel/framework#23475. |
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
Added to resources/lang/en/validation.php
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.
The text was updated successfully, but these errors were encountered: