Comments (9)
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.
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.
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.
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.
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.
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.
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.
I'm pretty much OK with this general idea.
from ideas.
PR submitted: laravel/framework#23475.
from ideas.
Related Issues (20)
- [Proposal] Easy way to cancel notifications at time of sending
- Rendering Blade Components Outside of Views HOT 1
- Assert that a component has rendered on a page
- Service Container | Contextual Binding of the Methods
- [REQUEST] Enable flush method from dynamoDB cache HOT 3
- [Feature] Enable flush method from dynamoDB
- [Proposal] Support AOP with php8 Attribute
- laravel join select max of joined column
- Ability to get event name which fires in Eloquent booted method HOT 1
- Make `php artisan migrate:reset` work with *-schema.dump
- Size File Validation only deals with Kilobytes
- Suggesting that ".env.testing" file be added to .gitignore HOT 2
- add native support for dev containers HOT 3
- [9.x] Rename firstOrNew() to firstOrMake()
- Set property to a config value via an Attribute HOT 1
- Add support to get more information on the job class
- Allow pausing batches
- afterCommit on Queueable Anonymous Event Listeners
- Upsert for related models
- Command option to run scheduler in a particular timezone
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ideas.