Skip to content

Commit

Permalink
[5.5] Make Request::validate() return value of parent key (#20974)
Browse files Browse the repository at this point in the history
*              make Request::validate return values of parent key in case rules has nested

* re-order

*    use helper

*         handle different places where validate returns data
  • Loading branch information
themsaid authored and taylorotwell committed Sep 5, 2017
1 parent 213312a commit 866e465
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ protected function failedAuthorization()
*/
public function validated()
{
return $this->only(array_keys(
$this->container->call([$this, 'rules'])
));
$rules = $this->container->call([$this, 'rules']);

return $this->only(collect($rules)->keys()->map(function($rule){
return str_contains($rule, '.') ? explode('.', $rule)[0] : $rule;
})->unique()->toArray());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function registerRequestValidate()
Request::macro('validate', function (array $rules, ...$params) {
validator()->validate($this->all(), $rules, ...$params);

return $this->only(array_keys($rules));

return $this->only(collect($rules)->keys()->map(function($rule){
return str_contains($rule, '.') ? explode('.', $rule)[0] : $rule;
})->unique()->toArray());
});
}
}
10 changes: 6 additions & 4 deletions src/Illuminate/Foundation/Validation/ValidatesRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public function validateWith($validator, Request $request = null)

$validator->validate();

return $request->only(
array_keys($validator->getRules())
);
return $request->only(collect($validator->getRules())->keys()->map(function($rule){
return str_contains($rule, '.') ? explode('.', $rule)[0] : $rule;
})->unique()->toArray());
}

/**
Expand All @@ -46,7 +46,9 @@ public function validate(Request $request, array $rules,
->make($request->all(), $rules, $messages, $customAttributes)
->validate();

return $request->only(array_keys($rules));
return $request->only(collect($rules)->keys()->map(function($rule){
return str_contains($rule, '.') ? explode('.', $rule)[0] : $rule;
})->unique()->toArray());
}

/**
Expand Down

0 comments on commit 866e465

Please sign in to comment.