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 Get Only Soft Deleted Records in Laravel 9

How to Get Only Soft Deleted Records in Laravel 9 ?

June 20, 2022June 20, 2022

In this article we will learn to get only soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 we learnt to delete the file without actually deleting from database and sometimes we want to show only that records…

Read More
Laravel How to Run a Background Queue Job or Request in Laravel

How to Run a Background Queue Job or Request in Laravel ?

May 17, 2022August 20, 2022

In any website running a queue can help to increase the runtime performance of any application by running a background queue job or request in laravel. Queues are much helpful while our application performs bulk actions like mailing to thousand of users or running a heavy tasks. In laravel we…

Read More
Php make a component in laravel 10

How to create component in Laravel 11 ?

July 14, 2024July 14, 2024

In this tutorial i will show you simple html based component in Laravel 11 using blade.Creating reusable components is essential in contemporary web development, enabling developers to streamline code and enhance efficiency. Laravel 11, a leading PHP framework, offers a robust component-building system. Similar to Angular or React, Laravel provides…

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

  • August 2025
  • 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

  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version