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

Validation rule "in" doesn't work with "sometimes" #16711

Closed
vigneshgurusamy opened this issue Dec 8, 2016 · 7 comments
Closed

Validation rule "in" doesn't work with "sometimes" #16711

vigneshgurusamy opened this issue Dec 8, 2016 · 7 comments

Comments

@vigneshgurusamy
Copy link
Contributor

  • Laravel Version: 5.2.45, 5.3.26
  • PHP Version: 7.0.13

Description:

There is an inconsistency when I use validation rule in with and without sometimes.

Steps To Reproduce:

Data for validation

$data = [
    "response" => [
            [
                "choices" => [40]
            ]
     ]
];

Rule without sometimes

$rules = [
    "response.*.choices" => "required|array|in:37,38,39"
];

If I run this rule against the data, it fails and produces a message like

array:1 [
  0 => "The selected response.0.choices is invalid."
]

But if I use sometimes along with other validation rule for choices, the validation doesn't fail as expected.

$rules = [
    "response.*.choices" => "sometimes|required|array|in:37,38,39"
];

After debugging, I found the root cause for this issue. The key response.0.choices does not exists, when validator checks for existence in dot notation data array.

https://github.com/laravel/framework/blob/5.2/src/Illuminate/Validation/Validator.php#L590

Right now I'm using this work around to solve this issue

$rules = [
    "response.*.choices" => "sometimes|required|array",
    "response.*.choices.*" => "required|in:37,38,39"
];
@themsaid
Copy link
Member

"response.*.choices" => "required|array|in:37,38,39" this is wrong, you're validating the array while you need to validate the values:

$rules = [
    "response.*.choices" => "sometimes|required|array",
    "response.*.choices.*" => "required|in:37,38,39"
];

this is correct.

@vigneshgurusamy
Copy link
Contributor Author

@themsaid Let say the array of choices should be a string

$data = [
    "response" => [
            [
                "choices" => [40]
            ]
     ]
];

$rules = [
    "response.*.choices" => "sometimes|required|string"
];

Validation doesn't fail for this rule set too.

@themsaid
Copy link
Member

themsaid commented Dec 12, 2016

You need to use this:

    $rules = [
        "response.*.choices" => "sometimes|required",
        "response.*.choices.*" => "string",
    ];

All arrays will be flattened inside the validator, that's why you need to handle this edge case in a special way, so your last work around is the way to go yes.

@vigneshgurusamy
Copy link
Contributor Author

@themsaid In my previous example, I was trying to demonstrate the sometimes rule does not work with string rule too, if the data was sent as an array.

Now I want to the validate the response.*.choices as string not as an array of string

Expected data structure

$data = [
    "response" => [
            [
                "choices" => "NY"
            ]
     ]
];

Instead of string, what if the response.*.choices attribute were sent as an array.

Received data structure

$data = [
    "response" => [
            [
                "choices" => ["NY"]
            ]
     ]
];

Should string validation rule fail or not?

@vigneshgurusamy
Copy link
Contributor Author

@themsaid do you need any additional info to replicate this issue or should I need to create new issue for my second scenario?

@themsaid
Copy link
Member

I've submitted a fix for that: #16826

Thank you :)

@vigneshgurusamy
Copy link
Contributor Author

@themsaid OSM!!!. This fix resolved my issue on both scenarios.

this fix will be merged to 5.2 as well?

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

No branches or pull requests

2 participants