Skip to content

Commit

Permalink
Fixed nested rules in validated data
Browse files Browse the repository at this point in the history
  • Loading branch information
ttsuru committed Aug 7, 2018
1 parent a09b0e3 commit 8af6002
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
7 changes: 2 additions & 5 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Foundation\Http;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Contracts\Container\Container;
Expand Down Expand Up @@ -173,11 +172,9 @@ protected function failedAuthorization()
*/
public function validated()
{
$rules = $this->container->call([$this, 'rules']);
$rules = $this->getValidatorInstance()->getRules();

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

/**
Expand Down
12 changes: 4 additions & 8 deletions src/Illuminate/Foundation/Validation/ValidatesRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Foundation\Validation;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -41,11 +40,10 @@ public function validateWith($validator, Request $request = null)
public function validate(Request $request, array $rules,
array $messages = [], array $customAttributes = [])
{
$this->getValidationFactory()
->make($request->all(), $rules, $messages, $customAttributes)
->validate();
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
$validator->validate();

return $this->extractInputFromRules($request, $rules);
return $this->extractInputFromRules($request, $validator->getRules());
}

/**
Expand All @@ -57,9 +55,7 @@ public function validate(Request $request, array $rules,
*/
protected function extractInputFromRules(Request $request, array $rules)
{
return $request->only(collect($rules)->keys()->map(function ($rule) {
return Str::contains($rule, '*') ? explode('.', $rule)[0] : $rule;
})->unique()->toArray());
return $request->only(array_keys($rules));
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,17 @@ public function validate()

$results = [];

$rules = collect($this->getRules())->keys()->map(function ($rule) {
return Str::contains($rule, '*') ? explode('.', $rule)[0] : $rule;
})->unique();
$keys = array_keys($this->getRules());
$input = $this->getData();

foreach ($rules as $rule) {
Arr::set($results, $rule, data_get($this->getData(), $rule));
$placeholder = new \stdClass();

foreach ($keys as $key) {
$value = data_get($input, $key, $placeholder);

if ($value !== $placeholder) {
Arr::set($results, $key, $value);
}
}

return $results;
Expand Down
30 changes: 27 additions & 3 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,24 @@ public function test_validated_method_returns_the_validated_data()

public function test_validated_method_returns_the_validated_data_nested_rules()
{
$payload = ['nested' => ['foo' => 'bar', 'baz' => ''], 'array' => [1, 2]];
$payload = ['nested' => ['foo' => 'bar', 'with' => 'extras']];

$request = $this->createRequest($payload, FoundationTestFormRequestNestedStub::class);

$request->validateResolved();

$this->assertEquals(['nested' => ['foo' => 'bar'], 'array' => [1, 2]], $request->validated());
$this->assertEquals(['nested' => ['foo' => 'bar']], $request->validated());
}

public function test_validated_method_returns_the_validated_data_nested_array_rules()
{
$payload = ['nested' => [['bar' => 'baz', 'with' => 'extras'], ['bar' => 'baz2', 'with' => 'extras']]];

$request = $this->createRequest($payload, FoundationTestFormRequestNestedArrayStub::class);

$request->validateResolved();

$this->assertEquals(['nested' => [['bar' => 'baz'], ['bar' => 'baz2']]], $request->validated());
}

/**
Expand Down Expand Up @@ -189,7 +200,20 @@ class FoundationTestFormRequestNestedStub extends FormRequest
{
public function rules()
{
return ['nested.foo' => 'required', 'array.*' => 'integer'];
return ['nested.foo' => 'required'];
}

public function authorize()
{
return true;
}
}

class FoundationTestFormRequestNestedArrayStub extends FormRequest
{
public function rules()
{
return ['nested.*.bar' => 'required'];
}

public function authorize()
Expand Down
21 changes: 16 additions & 5 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4007,17 +4007,28 @@ public function testValidateReturnsValidatedData()

public function testValidateReturnsValidatedDataNestedRules()
{
$post = ['nested' => ['foo' => 'bar', 'baz' => ''], 'array' => [1, 2]];
$post = ['nested' => ['foo' => 'bar', 'with' => 'extras', 'type' => 'admin']];

$rules = ['nested.foo' => 'required', 'array.*' => 'integer'];
$v = new Validator($this->getIlluminateArrayTranslator(), $post, ['nested.foo' => 'required']);
$v->sometimes('nested.type', 'required', function () {
return false;
});
$data = $v->validate();

$v = new Validator($this->getIlluminateArrayTranslator(), $post, $rules);
$v->sometimes('type', 'required', function () {
$this->assertEquals(['nested' => ['foo' => 'bar']], $data);
}

public function testValidateReturnsValidatedDataNestedArrayRules()
{
$post = ['nested' => [['bar' => 'baz', 'with' => 'extras', 'type' => 'admin'], ['bar' => 'baz2', 'with' => 'extras', 'type' => 'admin']]];

$v = new Validator($this->getIlluminateArrayTranslator(), $post, ['nested.*.bar' => 'required']);
$v->sometimes('nested.*.type', 'required', function () {
return false;
});
$data = $v->validate();

$this->assertEquals(['nested' => ['foo' => 'bar'], 'array' => [1, 2]], $data);
$this->assertEquals(['nested' => [['bar' => 'baz'], ['bar' => 'baz2']]], $data);
}

protected function getTranslator()
Expand Down

0 comments on commit 8af6002

Please sign in to comment.