-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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] Resolve parameter fields with asterisks when validating arrays #11927
Conversation
* Replace each field which has asterisks with the numeric keys of the given attribute. | ||
* | ||
* @param array $fields | ||
* @param string $attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cs
@GrahamCampbell done |
+1 |
1 similar comment
+1 |
Lots of duplicate code here. ;( |
@GrahamCampbell could you be more specific please? I know the tests are not DRY at all, I can refactor them I guess. How about the implementation code? |
@sebdesign maybe in the |
Thank you for your feedback @garygreen! I will revise my code as soon
|
@GrahamCampbell @taylorotwell @garygreen I've refactored! No more ugly copy-pasta. |
MUCH better, good work ! 😄 |
@garygreen thank you! I think the TaylorOtwell™ style comment was the hardest part. :) |
+1 |
if (preg_match_all('/\.(\d+)\./', $attribute, $keys)) { | ||
array_shift($keys); | ||
|
||
return array_collapse($keys); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling helpers should be avoided.
return Arr::collapse($keys);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucasmichot fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Looks like this needs re-basing? Did you want to make any changes to the tests as you mentioned in the original PR? |
Move some methods around, improve naming, and remove repeated method calls, because elegant code is good code. Also, determine which rules need their parameters fields normalized. For example, we don't want to replace asterisks with numbers on regex rules.
Validation Rule : Error Message Can anyone help me why its not validation array and why its showing error message like this ? |
When validating arrays with rules that accept other fields as parameters, like
required_*
, the field names should reflect the structure of the array we are validating against. E.g.:This rule would NOT check if
last_name
is present in each person, but if it is present in the root array data. While this is a useful feature, we need a way to accept fields as parameters which are scoped to the current item when we perform array validation.The most natural way would be to specify the fields with
*
character like we do in the rule keys. Unfortunately this doesn't work in 5.2, so this PR addresses and fixes that issue. It also works on multidimentional (multiple*
) array structures. Here is an example:There is also the issue of the missing array keys, that should be addressed in #11885 .
For now all my tests are specifying
'field' => null
when we need to check for a field, but this should be fixed to work even if the needed field is missing from the data.