Laravel Validation Rules If Value Exists in Another Field Array
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel Dynamic Validation Rules Based on Array Values
In this blog post, we'll discuss how to implement dynamic validation rules in Laravel based on the values found within an input array. This example aims to show you exactly how to make a form field mandatory if another related array contains specific data without having to extend the class. We will use Laravel 5.4 for this tutorial but the concept should work across versions.
First, we need to understand the problem statement: You want to validate a form where one of the fields is only required if a certain value appears in another field. In our case, you have a checkbox array for program options and another input field called 'music_instrument.' The validation rule requires that the 'music_instrument' field be mandatory when the user selects an option from the 'program' array related to music.
We'll work through the process step by step:
1\. Start off with your HTML form like this:
<form action="" method="post">
<fieldset>
<input name="program[]" value="Anthropology" type="checkbox">Anthropology
...
<input name="music_instrument" type="text" value="">
<button type="submit">Submit</button>
</fieldset>
</form>
2\. Next, you'll need to pass the request variables to Laravel's validation rules:
$request->all()
3\. Write your validation rules in Laravel code:
$validator = Validator::make($request->all(),[
'program' => 'required',
'music_instrument' => 'required_if:program,in_array:Music'
]);
The above code is a standard approach to Laravel validation rules. However, it may not produce the desired results in this case. The reason for this issue is that Laravel processes the submitted form data and doesn't maintain array indexes for non-selected checkboxes. This causes some inconsistencies when validating based on the values of these fields.
4\. To overcome this problem, you can try using a different approach:
$validator = Validator::make($request->all(),[
'program' => 'required',
'music_instrument' => array('required_if:program,contains:Music'),
]);
This method works to a certain degree. However, it has one drawback: if there are multiple selected options and one of them is Music, this validation will still fail since the entire 'program' list isn't considered for the condition check. In such cases, we need to create our own custom rule to validate music_instrument only when there is a value in array program that contains Music.
5\. To achieve this, you can build your validation based on Laravel collection functions:
$validator = Validator::make($request->all(),[
'program' => 'required',
'music_instrument' => array('required_if:array_intersect(collect($request->input('program')), collect(['Music']))->isNotEmpty()'),
]);
This custom rule checks if the intersection of the 'program' options and an array containing only "Music" is not empty. If there are multiple selected values in the 'program' array, it will ensure that music_instrument is required only when one of them is Music.
Your final code should now be working as expected! Note that, if you have any additional requirements or constraints, you can modify this custom rule to fit your needs more efficiently.
In conclusion, we have tackled the problem of implementing dynamic validation rules in Laravel based on values within an input array by combining existing validation features and creating a custom rule. This approach allows for flexibility and precise control over your application's validation logic while keeping your code maintainable and readable.