Giter VIP home page Giter VIP logo

Comments (9)

vlakoff avatar vlakoff commented on July 18, 2024 1

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);
});

from ideas.

vlakoff avatar vlakoff commented on July 18, 2024

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());
});

from ideas.

robjbrain avatar robjbrain commented on July 18, 2024

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

Otherwise appreciate the response and amendments.

from ideas.

franzliedke avatar franzliedke commented on July 18, 2024

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.

from ideas.

vlakoff avatar vlakoff commented on July 18, 2024

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.

from ideas.

franzliedke avatar franzliedke commented on July 18, 2024

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.

from ideas.

vlakoff avatar vlakoff commented on July 18, 2024

Refs laravel/docs#1833.

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

from ideas.

taylorotwell avatar taylorotwell commented on July 18, 2024

I'm pretty much OK with this general idea.

from ideas.

vlakoff avatar vlakoff commented on July 18, 2024

PR submitted: laravel/framework#23475.

from ideas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.