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 Laravel where condition with example

How to use laravel where condition with example ?

January 15, 2022January 18, 2022

Laravel eloquent provides multiple ways to build the query one the of the feature of laravel eloquent is creating dynamic query based on condition or complicated queries.In this article i will show you to build where condition in laravel with example. I will show you multiple example to create where…

Read More
Php How to Upload image with preview in Laravel 5/6 with example

How to Upload image with preview in Laravel 5/6 with example ?

June 12, 2022June 25, 2022

In this tutorial we will learn to Upload image with preview in laravel or for any website can be basic requirement like set up a product picture or adding profile to for verification. Laravel 5/6 provides robust functionality to upload and process the image with security. Laravel simply use Request…

Read More
Laravel append URL query params to pagination laravel

How to Append URL Query Params to Pagination Laravel ?

November 17, 2023March 16, 2024

In this guide, we’ll focus on improving user experience by append URL query params to pagination laravel links. Pagination plays a vital role in web applications, enabling users to navigate through extensive datasets.This not only enhances usability but also ensures a smoother transition between pages. Append URL query params to…

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

  • The Transformative Power of Education in the Digital Age
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version