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

[10.x] Fix Laravel Missing Validation rules not working with nested array data #49444

Conversation

aabadawy
Copy link
Contributor

@aabadawy aabadawy commented Dec 20, 2023

  This PR let next validation rules work fine with nested array data dependacies.

  • missing_if
  • missing_unless
  • missing_with
  • missing_with_all

I also add assertions for each test validation rule of them

After this PR

this next validation rule will throw validation errors

use Illuminate\Support\Facades\Validator;

$rules = ['foo.*.bar' => ['missing_if:foo.*.fez,1']];

$data = [
    'foo' =>  [
        [ 
            'bar' => 'should be missing',
            'fez' => 1 
        ],
        [
            'bar' => 'can be filled',
            'fez' => 10,
        ]
    ]
];

Validator::validate($data,$rules);
# this will throw validation exception with message The foo.0.bar field must be missing when foo.0.fez is 1

Before this PR

These rules escape the validation without throwing any validation error, As the asterisk had not been replaced with the current array row index.

@taylorotwell
Copy link
Member

Your array isn't nested enough. You don't have have foo.*.bar, you just have foo.bar.

@aabadawy
Copy link
Contributor Author

aabadawy commented Dec 20, 2023

@taylorotwell I edited The Description Example, My Bad.

You can check that: missing_if and other missing validation rules are not exist at dependentRules
Which is my PR fix.
Also you can see

if ($this->dependsOnOtherFields($rule)) {

image

this method relace the asterisks with correct key.
How it will be replaced if the (missing_if,missing_unless,..etc) not exists in $dependentRules property ?

@aabadawy aabadawy deleted the fix-missing-validation-rule-with-nested-array-data branch December 21, 2023 16:40
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

2 participants