Skip to content
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

Closed
wants to merge 2 commits into from
Closed

[5.2] Make custom validation rules to be dependent rules #14772

wants to merge 2 commits into from

Conversation

uxweb
Copy link
Contributor

@uxweb uxweb commented Aug 12, 2016

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:

'things.*.first_field' => 'greater:things.*.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:

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.

…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 uxweb changed the title Make custom validation rules to be dependent rules [5.2] Make custom validation rules to be dependent rules Aug 12, 2016
@themsaid
Copy link
Member

@uxweb Since you're changing the Factory::extend() method signature I believe this should be submitted to 5.3 instead.

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.

@uxweb
Copy link
Contributor Author

uxweb commented Aug 13, 2016

@themsaid Sure, i'll try to add a test 👍, thanks

@taylorotwell
Copy link
Member

Holding off on this implementation for now.

@briano-jabico
Copy link

@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!

@neilcrookes
Copy link

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:

data = [
    dates => [
        2017-04-15 => [from => 09:00, to => 17:00],
        2017-04-16 => [from => 09:00, to => 17:00],
        2017-04-17 => [from => 09:00, to => 17:00],
        2017-04-18 => [from => 09:00, to => 17:00],
    ]
]

It would be great if we could write a rule like:

rules = [
    dates.*.to => time_after:dates.*.from
]

But since the custom validation rule is not one of dependentRules, the asterisk in the parameters sent to the custom rule doesn't get replaced with the key.

@pavel-mironchik
Copy link
Contributor

@briano-jabico look at 5.4.18 - #18654

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

Successfully merging this pull request may close these issues.

6 participants