Validate multiple (combined) values / attributes in Laravel

Submitted by sgelbart - 2 years ago

Let's say that your validation is conditional on multiple rules.... This is a bit tricky because it's not obvious how to get the other rules passed to the validator. Here's how you'd do it

Validator::extend('combined_under_3', function ($attribute, $value, $parameters, $validator)
    //this will return all rules with values passed to this validator instance!
    $allValues = $validator->getData();
    $total = $attribute + $allValues['second-value'];

    return $total < 3;

//also, don't forget to add a custom validation message!
comments powered by Disqus