Laravel Complex Conditional Validator

9 months ago

Imagine that, you have two fields to validate.

  • first
  • second

Validation Rules:

  • The first field is required.
  • The second field is required IF the first field has value and that value is equal foo. Then, set the minimum value is 5, maximum value is 50.


    $validator = Validator::make(request()->all(), [
        'first' => 'required',

    $validator->sometimes('second', 'required_if:first,true|min:5|max:50', function ($input) {
        return $input->first == 'foo';