framework: [FormRequest] validated() method causes the validation to be called again.
- Laravel Version: 5.7.5
- PHP Version: 7.2.9
- Database Driver & Version: MySQL 5.7.23
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 941c8c7be2ad4441cdba12053c15f5dc46980707 commit breaks this.
Steps To Reproduce:
- Controller:
use App\Http\Requests\ClientStoreRequest;
class ClientController extends Controller
{
public function store(ClientStoreRequest $request)
{
$this->clientService->createClient($request->validated());
return redirect()->route('app.clients')->with('status', 'Client was successfully created!');
}
}
- FormRequest:
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class ClientStoreRequest extends FormRequest
{
public function rules()
{
return [
'full_name' => 'required|string|max:50',
'phone' => [
'required',
'max:50',
'regex:/^(\+[0-9]+)$/',
Rule::unique('clients')->where(function($query){
return $query->where('account_id', auth()->user()->account_id);
})
],
'email' => 'nullable|email|max:50',
'info' => 'max:200'
];
}
}
- Debugbar:
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.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 23 (16 by maintainers)
Commits related to this issue
- Using Validator::validate() — committed to laravel/framework by ttsuru 6 years ago
- Fix duplicate validation issue (#26604) This will prevent validation from happening twice. Fixes https://github.com/laravel/framework/issues/25736 — committed to laravel/framework by driesvints 6 years ago
- Fix duplicate validation issue (#26604) This will prevent validation from happening twice. Fixes https://github.com/laravel/framework/issues/25736 — committed to illuminate/validation by driesvints 6 years ago
I think we should fix this in the 5.7 release and not change the contract.
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.
Can this be merged into 5.7? This is a critical fault introduced within the 5.7 released and it sounds like I am not the only one who is forced to use 5.6 until it is resolved.
@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)
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.
@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.