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

Fix sometimes validation for nested arrays #4076

Conversation

jarektkaczyk
Copy link
Contributor

Fixes not working 'sometimes' optional check for nested arrays.

@taylorotwell
Copy link
Member

Need more examples of what this fixes. Paste something that was broken before and is now fixed. Also there are spacing problems in this pull.

@jarektkaczyk
Copy link
Contributor Author

The issue came up here: http://stackoverflow.com/questions/22892058/laravel-sometimes-validator-fails-with-nested-arrays

Imagine form with array data:
Form:

Then:
$rules = ['user.phone' => 'required'];
$data = ['user' => ['phone' => null]];

Validator::make($data, $rules)->passes(); // false

BUT
$rules = ['user.phone' => 'sometimes|required'];
$data = ['user' => ['phone' => null]];

Validator::make($data, $rules)->passes(); // true

Validator checking optional rule can't get to the nested array and find proper key, thus returns always true. Check the tests, you'll find example there.

About spacing - there were indents with spaces, but in latest push there are tabs.

@jarektkaczyk
Copy link
Contributor Author

Although previous solution worked, it was overkill. In fact one tiny fix in Validator - using array_dot, solves the issue.

@@ -307,7 +307,7 @@ protected function getValue($attribute)
protected function isValidatable($rule, $attribute, $value)
{
return $this->presentOrRuleIsImplicit($rule, $attribute, $value) &&
$this->passesOptionalCheck($attribute);
$this->passesOptionalCheck($attribute);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is still wrong here.

@GrahamCampbell
Copy link
Member

Also, please squash this when you are done. 7 commits or more will be a bit much. :)

@jarektkaczyk
Copy link
Contributor Author

Squashed, indentations corrected. Check it now please.

@GrahamCampbell
Copy link
Member

👍 from me.

@ghost
Copy link

ghost commented Jun 18, 2014

this definitely should be included in public release

@ghost
Copy link

ghost commented Jun 18, 2014

Also is worth noting that documentation is wrong about sometimes implementation.

$v = Validator::make($data, array(
    'email' => 'sometimes|required|email',
));

Clearly here sometimes is pointless used in conjunction with required as the latter seems to have precedence.
Example should be:

$v = Validator::make($data, array(
    'email' => 'sometimes|email',
));

taylorotwell added a commit that referenced this pull request Jul 13, 2014
…idation-for-nested-arrays

Fix sometimes validation for nested arrays
@taylorotwell taylorotwell merged commit 2340d88 into laravel:4.1 Jul 13, 2014
@jarektkaczyk jarektkaczyk deleted the bugfix/Fixes-sometimes-validation-for-nested-arrays branch July 18, 2014 09:26
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

3 participants