Laravel validation a column required if some condition true. A form field required with other condition true. Laravel a field required depend on other field value. Laravel required_if and Rule::requiredIf method. required_if in laravel 7, laravel 8, laravel 9.
Laravel required_if depend another column
If you want to required a field value depend upon another field value use required_if condition. See below example amount field required when type = 2.
Validator::make($request->all(), [ 'amount' => 'required_if:type,2' ]);
Laravel validation Rule::requiredIf rule
When you have more complex condition in required_if rule use Rule::requiredIf method. You want to make a field required validation with another condition true. requiredIf method make field required when its pass true boolean value.
use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; Validator::make($request->all(), [ 'role_id' => Rule::requiredIf($request->user()->is_admin), ]); Validator::make($request->all(), [ 'amount' => Rule::requiredIf(fn () => ($request->type == 2)), ]);
In our example first condition role_id required if request user have admin role and in second condition amount is required when type field have 2 value.