-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[FormRequest] validated() method causes the validation to be called again. #25736
Comments
I'm sorry.
|
1 is breaking change method to |
@ankur91 Sorry about the duplication I want to add that the scope of this issue is not limited to a few database queries being done twice. While annoying and inefficient, it isn't a breaking fault. However, with regards to validation that uses One Time Password (OTP) keys or passwords, this issue becomes more of a breaking fault rather than an inconvenience. (Example, Google Recaptcha) |
Hi, if you're not working on this one, please let me know, because I'd like to get this as my first contribution to Laravel 😃 |
@mohammedmanssour For me, this error is still a big problem in my app, which does not allow me upgrade to the latest version of Laravel. I also hope that this will be fixed soon. |
pull request #26417. |
Will be fixed once 5.8 is released. |
@driesvints @ttsuru I did a fresh Laravel install from master branch ( 5.8.* ) to test PR ( #26419 ), the issue still exist. Am I messing something? |
@ttsuru can you confirm that this is actually fixed? |
This problem is a big issue for a project I am working on..
|
@driesvints |
Please see my comment on said PR. |
Can this be merged into 5.7? |
The current solution can't be merged in 5.7 because it contains breaking changes. |
It is not a "breaking changes" it is a "Changed realization" like #26314 😄 |
Adding my voice to this completely breaking form requests, as others have said if you validate via a 3rd party api a double validation won't work. As well as any other expensive validation from database queries or custom methods. If you are really serious about waiting till 5.8 (3 months?) the you should update the docs to warn people not to use the example if they're using expensive api calls, databases queries or custom methods. |
By reading the comments in the closed PR ( #26417 ) for the 5.7 branch I understand the only breaking change is the added Can't we apply the fix to 5.7 the fix without changing the interface? This way we would not break any app relying on the validator interface. We have cases of methods that were not in the Contracts and were used throughout the framework. One case is the I don't think anyone relies on double validation for their own app. So the fix would in fact be the expected behavior. Hitting the database twice when using DB based validations, or calling an API twice when using a custom rule is enough reason to fix this behavior in the current release. If the solution is to wait, ok, I guess I don't have a choice. But I think we could consider to fix this issue in 5.7 as well. |
We're probably not going to fix this in the current 5.7 release. I realize that the current implementation isn't ideal but it's also not a critical bug. This will be fixed in the 5.8 release. |
The easiest temporary fix for anyone having this issue would be to extend the FormRequest class and intercept the validated() method e.g: namespace App\Http\Requests;
use \Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
class FormRequest extends LaravelFormRequest
{
/**
* The validated data
*
* @var array
*/
protected $validatedData;
/**
* Return the validated data.
*
* @return array
*
* @throws \Illuminate\Validation\ValidationException
*/
public function validated()
{
if ($this->fails()) {
throw new ValidationException($this);
}
return $this->validatedData ?: $this->validatedData = parent::validated();
}
} It's not how it should be fixed within Laravel obviously, but it's the easiest fix and should be compatible with any changes made between now and 5.8. |
I think we should fix this in the 5.7 release and not change the contract. |
I resent the original PR without the contract for 5.7 ^ |
This will prevent validation from happening twice. Fixes #25736
This will prevent validation from happening twice. Fixes laravel/framework#25736
Is this really fixed? I'm using laravel 5.8 and during a http test it happened to me.
The validation logic is as follow:
The test itself:
But when i run the tests, phpunit screams:
|
Description:
The problem is getting the retrieve the validated input data with method
validated()
. When it is called, it re-initiates the request validation. It seems like 941c8c7 commit breaks this.Steps To Reproduce:
Since the rules have a check in the database, we can see a duplicated query to the database, which means that the method rules() was called twice.
The text was updated successfully, but these errors were encountered: