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 Unique Validation in laravel 8

Unique validation on Update in Laravel with example

June 19, 2022November 17, 2023

In this article we will learn to handle the unique validation on update in laravel.As we know Laravel support validation for database operations. Laravel unique validation validates the table column uniqueness using unique validation. In this tutorial we will learn about the unique validation to table columns and also while…

Read More
Php How to Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel ?

June 27, 2022November 7, 2023

In this article we will learn to show flash success and error message in laravel. Flash messages are useful when we want to notify the user about their recent activity like submit a form or making any action on website so in this case it may multiple type of messages…

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

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version