Laravel conditional validation in a simple way

10 November , 2019   Blog

Laravel is a very great php framework to build websites 
and web applications
It has made developer`s life quite easy.
On most of the form, validation will work out of box
and it is very easy to validate request on laravel.

For example login validation for email and password will be

$request->validate([
'email' => 'required|email',
'password' => 'required|min:6|max:10'
]);
if ($validation->fails()) {
return response()->json($validation->errors(), 422);
}
Above validation will validate that email field is present on the request 
and it is of email type
where as for password,
it must also be present on the request and it shoud be of
minimun 6 characters
and can be of atmost 10 characters.

On most of the case above validation rule
will be sufficient to get started whereas
we will face such conditions where
we need to validation the field or
value of the field as per the request field.

This is being documented on  Laravel Validation Doc.



For example , in one of our real time problem,
we have to validate amount on the request as per the type of request.

$validation = Validator::make($request->all(), [
'amount' => 'required|numeric',
'type' => 'required|in:standard,premium'
]);
/*
* validation checking as per the type field on the request
* if type is standard, their range is 2,000 to 10,000
* else if type is premium, their range is 5,000 to 20,000
* */
$validation->after(function($validation) use ($request)
{
if($request->type === 'standard'){
$validation = Validator::make($request->all(), [
'amount' => 'required|numeric|min:2000|max:10000',
]);
}
elseif ($request->type === 'premium'){
$validation = Validator::make($request->all(), [
'amount' => 'required|numeric|min:5000|max:20000',
]);
}
});
// in case of failure, we will send 422 error
if ($validation->fails()) {
return response()->json($validation->errors(), 422);
}

Also if we use write custom message on the error bag, I perform like this:
In case of localized message,we need some extra codes.

/*
* validation checking as per the type field on the request
* if type is standard, their range is 2,000 to 10,000
* else if type is premium, their range is 5,000 to 20,000
* */
$validation->after(function($validation) use ($request)
{
if($request->type === 'standard'){
if($request->amount < 2000 || $request->amount > 10000){
$validation->errors()->add('amount',
'Standard type has transaction limit from 2,000 to 10,000 only');
}
}
elseif ($request->type === 'premium'){
if($request->amount < 5000 || $request->amount > 20000){
$validation->errors()->add('amount',
'Premium type has transaction limit from 5,000 to 20,000 only');
}
}
});
// in case of failure, we will send 422 error
if ($validation->fails()) {
return response()->json($validation->errors(), 422);
}

 

In such way,
Conditional and complex validation on laravel can be done on very easy way.

If you have further suggestions,
Feel free to comment.
Happy Coding

This blog is also featured on Dreamsys Blog

 

#programming
Share With