Validation rules required_if with other condition (Laravel 5.4)

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Mastering Nested Condition Validation Rules in Laravel 5.4 Introduction: In this comprehensive blog post, we will discuss the best practices for validating forms with nested conditions in Laravel 5.4, especially when handling scenarios that require specific rules to apply only under certain circumstances. We'll delve into customizing validation rules while adhering to proper coding structure and syntax. Let's start by exploring an issue faced by a developer working on a form requiring complex validation checks. The Issue: A developer is facing problems with validation rules concerning nested conditions in their Laravel application. They need to check if the 'external_id' field must be an integer only when the 'type_id' equals 3, while maintaining validations for other cases. We will now break down the code and offer a resolution to this issue. Original Code:
class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                return [
                    'type_id'     => 'required|integer',
                    'external_id' => 'required_if:type_id,==,3|integer',
                ];
        }
}
Problem Analysis: The above code does perform the required validation checks when 'type_id' equals 3. However, it doesn't work as expected when 'type_id' is not equal to 3. When a user selects another type_id (1 or 2), the system still enforces the integer requirement for external_id, which isn't accurate based on the conditional logic intended. Resolution: To tackle this situation, we need to separate the validation rules for different conditions. We can create a new array to store additional rules and perform multiple checks using Laravel 5.4's rule groups feature. Here's how our code will look after making these amendments:
class StoreRequest extends Request
{
        public function authorize(){
        return true;
        }

        public function rules(){
                $rules = [
                    'type_id'     => 'required|integer',
                    'external_id' => 'nullable',
                ];

                if ($request->input('type_id') == 3) {
                    $rules['external_id'] = 'required|integer';
                    return collect($rules)->merge([
                        'external_id' => Rule::unique('table_name')->where(function ($query) use ($request) {
                            $query->where('type_id', $request->input('type_id'));
                        }),
                    ])->all();
                }

                return collect($rules)->merge([
                    'external_id' => Rule::unique('table_name')->where(function ($query) use ($request) {
                        $query->where('type_id', $request->input('type_id'));
                    }),
                ])->all();
        }
}
In this code, we've initially defined the rules array and separate the validation rule sets for each condition. We use Laravel 5.4's unique() method to ensure that no duplicate values are allowed in the external_id column while keeping the necessary checks based on the type_id value. This ensures both accuracy and efficiency in our form validations. Conclusion: We have successfully tackled the issue with nested condition validation rules in Laravel 5.4 by breaking down the problem, analyzing the code, and implementing an efficient solution. By separating rule sets using Laravel's unique() method and making use of conditional statements, we can ensure consistent and accurate validation checks for various scenarios in our forms.