how to get the value of parameter passed in custom validation rule laravel 5

2.3k views Asked by At
The custom validation rule is :

Validator::extend('greater_than', function($attribute, $value, $parameters) {
    if (isset($parameters[0])) {
        return intval($value) > intval($parameter[0]);
    } else {
        return false;
    }
}

max_occupancy rule would then be:

'max_occupancy' => 'required|integer|max:100|greater_than:base_occupancy'

but the "$parameters" array returning is : array:1 [▼ 0 => "base_occupancy"]. so i am not getting the value of base_occupancy to check "greater_than" condition.

1

There are 1 answers

0
The Anh Nguyen On

Use $validator->getData(). It will return a key-value array that has the key are form input names and the value are their values.

In your case, fix like this:

Validator::extend('greater_than', function($attribute, $value, $parameters, $validator) {
    $all_form_data = $validator->getData();
    if (isset($all_form_data[$parameters[0]])) {
        return intval($value) > intval($all_form_data[$parameters[0]]);
    } else {
        return false;
    }
}