'Laravel how to stop validation after first error
I don't have a clue how to make laravel validate stop validation after first error occurs and then return only one error
Rules with val_ prefix are my custom rules.
I need to display only error for pesel field if the pesel field is empty('required' rule) Could anyone tell me how can I reach that ?
$this->validate($request, [
'pesel'=> 'bail|required|val_pesel',
'dane_os' => 'required',
'id_project' => 'required',
'imie' => 'required|val_imie',
'nazwisko'=>'required|val_nazwisko',
'nazwisko_matki'=>'required|val_nazwisko_matki'
]);
MY VALIDATION CODE
Validator::extend('val_pesel',function($attribute,$value,$parameters,$validator)
{
$val = DB::select('select * from `wyborca` where pesel = "'.$value.'" ; ');
if(empty($val))
{
return false;
}
else {
return true;
}
});
Validator::extend('val_imie',function($attribute,$value,$parameters,$validator)
{
$test = $validator->getData();
$pesel = $test['pesel'];
$imie = $test['imie'];
$val = DB::select('select * from `wyborca` where pesel = "'.$pesel.'" and imie = "'.$imie.'" ; ');
if(empty($val))
{
return false;
}
else {
return true;
}
});
Validator::extend('val_nazwisko',function($attribute,$value,$parameters,$validator)
{
$test = $validator->getData();
$pesel = $test['pesel'];
$nazwisko = $test['nazwisko'];
$val = DB::select('select * from `wyborca` where pesel = "'.$pesel.'" and nazwisko = "'.$nazwisko.'" ; ');
if(empty($val))
{
return false;
}
else {
return true;
}
});
Validator::extend('val_nazwisko_matki',function($attribute,$value,$parameters,$validator)
{
$test = $validator->getData();
$pesel = $test['pesel'];
$nazwisko_matki = $test['nazwisko_matki'];
$val = DB::select('select * from `wyborca` where pesel = "'.$pesel.'" and nazwisko_matki = "'.$nazwisko_matki.'" ; ');
if(empty($val))
{
return false;
}
else {
return true;
}
});
Validator::extend('vote_exists',function($attribute,$value,$parameters,$validator)
{
$test = $validator->getData();
$pesel = $test['pesel'];
$val = DB::select('select * from `glosy` where pesel = "'.$pesel.'" ; ');
if(empty($val))
{
return false;
}
else {
return true;
}
});
}
Solution 1:[1]
To stop validation if the first rule fails you should use bail, quote from docs
Stopping On First Validation Failure Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:
$this->validate($request, [
'title' => 'bail|required|unique:posts|max:255',
'body' => 'required',
]);
In this example, if the required rule on the title attribute fails, the unique rule will not be checked. Rules will be validated in the order they are assigned.
https://laravel.com/docs/5.5/validation
here is the github discussion https://github.com/laravel/framework/issues/4789#issuecomment-174837968
Solution 2:[2]
I know this question is old, but I had the same problem today, so I'll document what I found out:
In Laravel 8.30.0 or newer, there are 2 ways to do it:
1. Using a FormRequest
Set the following attribute in your FormRequest class:
protected $stopOnFirstFailure = true;
2. Using a Validator
Manually create a Validator, and call the stopOnFirstFailure(true) method on it, as in:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
// your validation rules
])->stopOnFirstFailure(true);
$validator->validate();
Solution 3:[3]
To redirect and stop validation after the first rule has failed you can use the 'bail' keyword. an example:
// $arr = ['form input var'=>'exact word to be used in error msg'] ***
$arr = ['coverletter'=>'Cover Letter','vitae'=>'CV','certificate'=>'Certificate','tesol'=>'Tesol','photo'=>'Photo'];
// $this->validate($data,$rules,$messages);
// we have 2 rules to validate, 'required' & 'mimetypes' so output messages for them must be in an array
foreach ($arr as $k=>$v) {
if($k !== 'photo') {
// Tesol, CV, Certificate must be in PDF format so we can loop through all 3 of them here
if(!Session::get($k) || $request->$k) {
// user has submitted before but is submitting again, we evaluate the request
// user has not submitted before, we evaluate the request
$this->validate($request,
[$k => 'bail|required|mimetypes:application/pdf'],
[$k . '.required' => $v . ' is required',
$k . '.mimetypes' => $v . ' must be PDF']);
}
}
if($k == 'photo') {
if(!Session::get($k) || $request->$k) {
$this->validate($request,
[$k => 'bail|required|mimetypes:image/jpg,image/jpeg'],
[$k . '.required' => $v . ' is required',
$k . '.mimetypes' => $v . ' must be of type jpeg']);
}
}
}
this will redirect back with your error message after the first rule failed.
Solution 4:[4]
For those wo are stuck with a Laravel Version < 8.30 and can not use the solution of @amade:
You can extend the default validator as follows:
1. Create a custom validator class extending the Laravel validator and overwrite the passes method.
<?php
namespace App\Validation;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;
class Validator extends Validator
{
protected bool $stopOnFirstFailure = false;
public function stopOnFirstFailure(bool $value = true): self
{
$this->stopOnFirstFailure = $value;
return $this;
}
// Pretty a much copy the the original code from the base class
// but added the lines around the third `break;`
public function passes()
{
$this->messages = new MessageBag;
[$this->distinctValues, $this->failedRules] = [[], []];
// We'll spin through each rule, validating the attributes attached to that
// rule. Any error messages will be added to the containers with each of
// the other error messages, returning true if we don't have messages.
foreach ($this->rules as $attribute => $rules) {
if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);
continue;
}
foreach ($rules as $rule) {
$this->validateAttribute($attribute, $rule);
if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);
break;
}
if ($this->shouldStopValidating($attribute)) {
break;
}
}
// ADDED LINES HERE
if ($this->stopOnFirstFailure && $this->messages->isNotEmpty())
{
break;
}
}
// Here we will spin through all of the "after" hooks on this validator and
// fire them off. This gives the callbacks a chance to perform all kinds
// of other validation that needs to get wrapped up in this operation.
foreach ($this->after as $after) {
$after();
}
return $this->messages->isEmpty();
}
}
The Code
if ($this->stopOnFirstFailure && $this->messages->isNotEmpty())
{
break;
}
Will check if there are any errors. In this case it will break out of the foreach ($this->rules as $attribute => $rules) loop iterating the attributes thus canceling the validation.
If you want to break after the first error for the current attribute you still should use bail!! This is intended! Otherwise it will evaluate all rules for a given attribute and stop afterwards. You can change this by evaluating the condition sooner or later in the validation process.
2. In an appropriate ServiceProviter replace the Laravel validator within the register method.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Validation\Validator;
class ValidationServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
/*
* Overwrite the validator with a derived one
*/
\Validator::resolver(function ($translator, $data, $rules, $messages) {
return new Validator($translator, $data, $rules, $messages);
});
/*
* Custom validation rules
*/
\Validator::extend('my_rule', function ($attribute, $value, $parameters, $validator) {
return my_condition_helper($value);
});
}
}
3. [OPTIONAL] Create custom FormRequest base-class:
<?php
namespace App\Http\Requests;
abstract class FormRequest extends \Illuminate\Foundation\Http\FormRequest
{
protected bool $stopOnFirstValidationFailure = false;
protected function getValidatorInstance()
{
/** @var \App\Validation\Validator $validator */
$validator = parent::getValidatorInstance();
$validator->stopOnFirstFailure($this->stopOnFirstValidationFailure);
return $validator;
}
}
When you derive this FormRequest with your FormRequest implementations you will be able to control their behavior with the $stopOnFirstValidationFailure property.
Tested with Laravel 7
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dav |
| Solution 2 | Amade |
| Solution 3 | |
| Solution 4 |
