Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Conditionally validate request fields in Laravel

The usual way of validating request attributes in Laravel is by making Illuminate\Support\Facades\Validator instance with your static rules that never change like so.

<?php

namespace App\Http\Controllers\API\v1\Users;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Entities\Models\User;

class UserController extends Controller
{
    public function store(Request $request)
    {
        
        // validate incoming request
        
        $validator = Validator::make($request->all(), [
           'email' => 'required|email|unique:users',
           'name' => 'required|string|max:50',
           'password' => 'required',
           'subscription_type' => 'required'
       ]);
        
       if ($validator->fails()) {
            Session::flash('error', $validator->messages()->first());
            return redirect()->back()->withInput();
       }
        
    }
}

You pass in all the request attributes along with validation rules that you want to validate, as an array to the second argument of make method.

Now, there might be a scenario where you might want to validate some fields only if certain conditions met. Meaning, you want to make validation conditional. How would you do this?

Using sometimes method

This can be done by using sometimes on the validator instance. So, for instance, in the previous example, if you want to validate the payment_method attribute only when the subscription_type is set to premium, you can do it like so.

$validator->sometimes('payment_method', 'required', function ($input) {
    return $input->subscription_type === 'premium';
});

Here, the $input parameter passed to the Closure will be an instance of Illuminate\Support\Fluent and can be used to access your input and files.

To take it further, you can add conditional validations for several fields at once like so.

$validator->sometimes(['payment_method', 'card_number'], 'required', function ($input) {
    return $input->subscription_type === 'premium';
});
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?