Laravel

Use of Laravel validation sometimes with request class

Share your learning

Today we are going to understand Laravel validation Sometimes rule with request class.

Few days back I was required to use required_if rule while using a laravel validations but required_if not work like that how I want it to be.

Why do we need Laravel validation sometimes?

Here is my scenario, 

I had a select box field as service type with options free and paid. So, if I select the free option then I will not need a price input field otherwise price should be required.

For this case I tried to use the required_if laravel validation rule. But I also need that price to be numeric only if it is required. That means I need to use multiple validation rules depending on the required_if rule.

One more thing, I was using a validation request class that time.

Example of Laravel validation sometimes

So, after some research I found that I can use the Laravel validation Sometimes rule by extending a method in my validation request class as given below. 

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ServiceRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */    public function rules()
    {
        return  [
            'name' => 'required|max:191',
            'service_type' => 'required'
        ];
    }


    protected function getValidatorInstance(){
        $validator = parent::getValidatorInstance();

        $validator->sometimes('price', 'required|numeric', function($input)
        {
            return $input->service_type === ‘paid’;
        });

        return $validator;
    }
}

Here we can see that we get the parent validator instance and then apply our rule on it. Now if I select the service type paid only then price will be required and must be numeric.

That’s it, Laravel validation Sometimes rule with request class is simple and easy to use.

Hope you find this article useful.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago