Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to use conditional validation Laravel 8

How to use conditional validation Laravel 8 / 9 ?

Aman Jain, December 17, 2021March 19, 2022

In Laravel sometime you want to exclude some validations on specific condition. Laravel provides exclude_if, exclude_unless and sometimes validation rules to validating some rules conditionally. In this tutorial we will take a simple example of has_city is checked then city name should be require and if its not checked then it will not validate .

So here is the syntax of exclude_if

 'exclude_if:field_name,boolean|other_validation_if_true', 

Example

use Illuminate\Support\Facades\Validator;

$validator = Validator::make($data, [
    'has_city'=>'required',
    'city' => 'exclude_if:has_city,true|required',
]);

Complex condition validation on multiple conditions

Sometime we need to check multiple condition or some logic to add the validation thus in that case we can use sometimes validation to add the condition validations in laravel.

In this example we will use age and gender , if age is greater then 18 then user must need to enter the gender and age is less then 18 then we will not validate the input

Syntax

$validator->sometimes('field_name','multiple_validations_to_apply', function ($input) {
    return boolean;
});

Example

$validator->sometimes('gender','require|in:Male,Female', function ($input) {
    return $input->age>18;
});

Here, we are checking if age is greater then 18 then gender is required and it must be either female or male.

We can also validate array conditionally as below

$validator->sometimes('user.*.address','require', function ($input, $item) {
    return $item->address_type === 'home';
});

There is another way too which we can implement in any Laravel version. Generally we create a set of rules for validation and then validate them by passing it to Validation::make method.

So, Here we will create a array rules and then we will add rule conditionally to this array as below

<?php 

$rulesList=["name"=>"required","age"=>"required"];

if($request->checked==1){
  $rulesList["city"] ="require";
}
if($request->age>18){
  $rulesList["gender"] ="require|in:Male,Female";
}

$validator = Validator::make($data, $rulesList);

Related

Php Laravel conditionallaravelvalidation

Post navigation

Previous post
Next post

Related Posts

Php How to Add or Update Index in Laravel Migration

How to Add or Update Index in Laravel Migration?

August 21, 2022March 16, 2024

Add or Update Index in Laravel Migration can be implement easily using the migrations libraries index method. Migrations provides almost all feature to create the database schema and create and update index in laravel migration. As We know database index is used to improve the speed of tables. Index can…

Read More

What is Laravel 8 middleware ?

November 6, 2021November 6, 2021

Laravel middleware is a mechanism to filter the http request and response. As the name implies that middleware so it’s middle between the request and response. we can change or validate request and response using the Laravel middleware. There is so many use cases of middleware like authentication, validating token,…

Read More
Devops install php 8 in ubuntu

How to install PHP 8.0 in Ubuntu 20.04 LTS ?

September 18, 2021November 5, 2023

In this tutorial we will learn to install PHP 8.0 on Ubuntu 20.04 LTS using PPA. PHP 8.0 is the latest version of php and it have many upgrades. Installation process is same as How to install PHP on Ubuntu? or How to Install multiple/different version of PHP? Step 1…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version