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.2] Resolve parameter fields with asterisks when validating arrays #11927

Merged
merged 4 commits into from
Feb 19, 2016
Merged

[5.2] Resolve parameter fields with asterisks when validating arrays #11927

merged 4 commits into from
Feb 19, 2016

Conversation

sebdesign
Copy link
Contributor

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.:

'person.*.first_name' => 'required_with:last_name'

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:

'person.*.first_name' => 'required_with:person.*.last_name'

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.

* Replace each field which has asterisks with the numeric keys of the given attribute.
*
* @param array $fields
* @param string $attribute
Copy link
Member

Choose a reason for hiding this comment

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

cs

@sebdesign
Copy link
Contributor Author

@GrahamCampbell done

@garygreen
Copy link
Contributor

+1

1 similar comment
@tomswinkels
Copy link
Contributor

+1

@GrahamCampbell
Copy link
Member

Lots of duplicate code here. ;(

@sebdesign
Copy link
Contributor Author

@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?

@garygreen
Copy link
Contributor

@sebdesign maybe in the validate() method it can decide what rules can have dot-format attributes in the parameters and pass thru the processed parameters, or maybe a new function ->parseParameters() or something. That will get rid of the need for $parameters = $this->replaceParameterFields($parameters, $attribute); in a lot of rules.

@sebdesign
Copy link
Contributor Author

Thank you for your feedback @garygreen! I will revise my code as soon
as possible.
On Feb 3, 2016 1:07 PM, "Gary Green" notifications@github.com wrote:

@sebdesign https://github.com/sebdesign maybe in the validate() method
it can decide what rules can have dot-format attributes and pass thru the
processed parameters, maybe ->parseParameters() or something. That will
get rid of the need for $parameters =
$this->replaceParameterFields($parameters, $attribute); in a lot of rules.


Reply to this email directly or view it on GitHub
#11927 (comment).

@sebdesign
Copy link
Contributor Author

@GrahamCampbell @taylorotwell @garygreen I've refactored! No more ugly copy-pasta.

@garygreen
Copy link
Contributor

MUCH better, good work ! 😄

@sebdesign
Copy link
Contributor Author

@garygreen thank you! I think the TaylorOtwell™ style comment was the hardest part. :)

@KoenRijpstra
Copy link

+1

if (preg_match_all('/\.(\d+)\./', $attribute, $keys)) {
array_shift($keys);

return array_collapse($keys);
Copy link
Contributor

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);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lucasmichot fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@taylorotwell
Copy link
Member

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.
@prafful-panwar
Copy link

prafful-panwar commented Oct 30, 2017

Validation Rule :
'event' => 'required|array', 'event.*.start_date_time' => 'required|date_format:Y-m-d|after:today', 'event.*.end_date_time' => 'required|date_format:Y-m-d|after:today', 'event.*.description' => 'required|spam_free',

Error Message
if ($messages->has('event')) {
$fieldId = 'event';
$errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
if ($messages->has('event..start_date_time')) {
$fieldId = 'event.
.start_date_time';
$errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
if ($messages->has('event..end_date_time')) {
$fieldId = 'event.
.end_date_time';
$errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
if ($messages->has('event..description')) {
$fieldId = 'event.
.description';
$errors [] = getErrorMessage($fieldId, $messages->first($fieldId));
}
Validation Message
{
"fieldld": "event..start_date_time",
"message": "The event.0.start_date_time field is required."
},
{
"fieldld": "event.
.end_date_time",
"message": "The event.0.end_date_time field is required."
},
{
"fieldld": "event.*.description",
"message": "The event.0.description field is required."
},

Can anyone help me why its not validation array and why its showing error message like this ?
I am using Laravel 5.5

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.

9 participants