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.4] Asterisk don't get replaced for custom rules on array validation #18647

Closed
wants to merge 0 commits into from

Conversation

rakesh-beedasy
Copy link
Contributor

@rakesh-beedasy rakesh-beedasy commented Apr 4, 2017

Illuminate\Validation\Validator@validateAttribute fails to replace '*' on array attributes since Illuminate\Validation\Validator@dependsOnOtherFields returns false for any custom extension.

When registering a custom validator one should be able to merge rules to Illuminate\Validation\Validator@$dependentRules

E.g.
Illuminate\Support\Facades\Validator::extendImplicit('required_if_in', App\ValidatorsRequiredIfIn::class);
Illuminate\Support\Facades\Validator::extendDependent('required_if_in', App\ValidatorsRequiredIfIn::class);
works with rules like
$rules = ['items.*.field' => 'required_if_in:items.*.otherfield,value1,value2']

@GrahamCampbell GrahamCampbell changed the title Asterisk don't get replaced for custom rules on array validation [5.4] Asterisk don't get replaced for custom rules on array validation Apr 4, 2017
@GrahamCampbell
Copy link
Member

Thanks. Please add tests. :)

@taylorotwell
Copy link
Member

@themsaid can you review this? is this really needed?

@themsaid
Copy link
Member

themsaid commented Apr 4, 2017

On it

@rakesh-beedasy
Copy link
Contributor Author

rakesh-beedasy commented Apr 4, 2017

I tried to extend Illuminate\Support\Facades\Validator with a custom implicit validation rule where a field would depend on another field's value. While it works fine when using single fields when defining my rules, it fails when defining arrays for validation.

Since I can't modify Illuminate\Validation\Validator::$dependentRules property, the '*' in the other field never gets replaced by the corresponding index for each array item.

$rules = ['items.*.field' => 'required_if_in:items.*.otherfield,value1,value2']

For each items '*' for 'field' gets correctly replaced by indexes 0,1,2 and so on but not for 'otherfield'.

E.g. if I have an array of 2 items, for each item that gets passed on to my custom validation rule function I get

$attribute as item.0.field, but $parameters[0] as item.*.otherfield  
$attribute as item.1.field, but $parameters[0] as item.*.otherfield 

when I'm expecting

$attribute as item.0.field, and $parameters[0] as item.0.otherfield  
$attribute as item.1.field, and $parameters[0] as item.1.otherfield 

@themsaid themsaid requested review from themsaid and removed request for themsaid April 4, 2017 18:05
@themsaid
Copy link
Member

themsaid commented Apr 4, 2017

This is useful for when you need to add a customer validation rule that require passing an implicit rule as a parameter:

Validator::make($data, ['*.username' => 'must_contain:*.id']);

This will allow the validator to convert *.id to (0.id, 1.id, etc..) so when you get the rule parameters you can use the explicit key to get the data of the other branch.

I've reviewed the PR and added some tests, if you'd like I can merge the PR with the changes I made.

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.

None yet

4 participants