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.8] Fix FormRequest validate not twice. #26486

Merged
merged 1 commit into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Illuminate/Foundation/Http/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class FormRequest extends Request implements ValidatesWhenResolved
*/
protected $errorBag = 'default';

/**
* The validator instance.
*
* @var \Illuminate\Contracts\Validation\Validator
*/
protected $validator;

/**
* Get the validator instance for the request.
*
Expand All @@ -77,7 +84,9 @@ protected function getValidatorInstance()
$this->withValidator($validator);
}

return $validator;
$this->setValidator($validator);

return $this->validator;
}

/**
Expand Down Expand Up @@ -172,7 +181,7 @@ protected function failedAuthorization()
*/
public function validated()
{
return $this->getValidatorInstance()->validated();
return $this->validator->validated();
Copy link
Member

Choose a reason for hiding this comment

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

What if you call this method before the validator is set? I think we should think this through more.

Copy link
Contributor Author

@ttsuru ttsuru Nov 12, 2018

Choose a reason for hiding this comment

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

@driesvints
Thanks for checking.
ValidatesWhenResolvedTrait::validateResolved() is called in FormRequestServiceProvider::boot() afterResolving.

So I think that it is normally set.

Choose a reason for hiding this comment

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

can't this be fixed by adding a simple if statement that can be added to the getValidatorInstance?

Copy link
Contributor Author

@ttsuru ttsuru Nov 12, 2018

Choose a reason for hiding this comment

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

@mohammedmanssour
I think so too, but getValidatorInstance called twice.
We require single validator instance.

}

/**
Expand All @@ -195,6 +204,19 @@ public function attributes()
return [];
}

/**
* Set the Validator instance.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return $this
*/
public function setValidator(Validator $validator)
{
$this->validator = $validator;

return $this;
}

/**
* Set the Redirector instance.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Foundation/FoundationFormRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Routing\UrlGenerator;
use Illuminate\Http\RedirectResponse;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Validation\Factory as ValidationFactoryContract;
Expand Down Expand Up @@ -67,6 +68,18 @@ public function test_validated_method_returns_the_validated_data_nested_array_ru
$this->assertEquals(['nested' => [['bar' => 'baz'], ['bar' => 'baz2']]], $request->validated());
}

public function test_validated_method_not_validate_twice()
{
$payload = ['name' => 'specified', 'with' => 'extras'];

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

$request->validateResolved();
$request->validated();

$this->assertEquals(1, FoundationTestFormRequestTwiceStub::$count);
}

/**
* @expectedException \Illuminate\Validation\ValidationException
*/
Expand Down Expand Up @@ -246,13 +259,36 @@ public function authorize()
}
}

class FoundationTestFormRequestTwiceStub extends FormRequest
{
public static $count = 0;

public function rules()
{
return ['name' => 'required'];
}

public function withValidator(Validator $validator)
{
$validator->after(function ($validator) {
self::$count++;
});
}

public function authorize()
{
return true;
}
}

class FoundationTestFormRequestForbiddenStub extends FormRequest
{
public function authorize()
{
return false;
}
}

class FoundationTestFormRequestHooks extends FormRequest
{
public function rules()
Expand Down