-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.2] Make custom validation rules to be dependent rules #14772
[5.2] Make custom validation rules to be dependent rules #14772
Conversation
…nt on other fields. This will allow to create a rule like this: First Field should be Greater than Second Field: ``` 'things.*.first_field' => 'greater:things.*.second_field' ``` This means that the * of the parameter of the rule (the other field) will be resolved as keys. Right now there is no way to tell to the validator that a custom extension should be dependent, so i will not resolve the keys from the dependent field. Letting the validator resolve this keys, will make a breeze to do this kind of custom validator: ``` Validator::extend('greater', function($attribute, $value, $parameters, $validator) { $other = Arr::get($validator->getData(), $parameters[0]); return $value >= $other; }, null, true); Validator::extendImplicit('greater', function($attribute, $value, $parameters, $validator) { $other = Arr::get($validator->getData(), $parameters[0]); return $value >= $other; }, null, true); // The last parameter on the extend and extendImplicit method tells that this is a dependent rule. // By default is false to get the normal behavior. ``` Now we can create implicit and explicit custom validation rules that can be dependent on other field.
@uxweb Since you're changing the I also believe it's better that you include a test in your PR, which will help others understand the issue easily as well as prevent it from appearing again in the future after any change. |
@themsaid Sure, i'll try to add a test 👍, thanks |
Holding off on this implementation for now. |
@taylorotwell Is something like this planned for 5.3.x or 5.4? As far as I know, this is still not possible. I'm making a budget editor where a user can edit many line_items at once, and I'd like to add a required_if_attribute validation rule to allow me to say that payment_date is required if the total is > $1,000. There's a few other ways I could do this, but none as reusable. Thanks in advance! |
I too would like to be able to create a custom validation rule for array data, that depends on the value of another field in the array. E.g. consider a list of from and to time fields, for a series of dates. The data would look as follows:
It would be great if we could write a rule like:
But since the custom validation rule is not one of |
@briano-jabico look at 5.4.18 - #18654 |
This little contribution allows custom validation rules to be dependent on other fields.
This will allow to create a rule like this:
First Field should be Greater than Second Field:
This means that the * in the parameter of the rule (the other field) will be resolved as keys.
Right now there is no way to tell to the validator that a custom extension should be dependent, so it will not resolve the keys from the dependent field.
Letting the validator resolve this keys, will make a breeze to do this kind of custom validator:
Now we can create implicit and explicit custom validation rules that can be dependent on other field.