0

I'm extending the Laravel Validation rules to create a rule for "between two user-supplied fields" - the between rule only handles "between two values".

Validator::extend('between_fields', function($attribute, $value, $parameters, $validator) {
    $data = $validator->getData();

    $min = array_get( $data, $parameters[0], $parameters[0] );
    $max = array_get( $data, $parameters[1], $parameters[1] );

    return $value >= $min && $value <= $max;
});

This works fine, but I'd then like to define an error message which uses the numeric values. I've set up a message like this:

'between_fields' => ':attribute must be between :min and :max.',

...which is how the existing between rule works, but the :min and :max don't get replaced with anything.

How do I pass the values for those from the Validator extension through to the message handler?

1 Answer 1

1

You have to that with the Validator::replacer()

Validator::replacer('between_fields', function($message, $attribute, $rule, $parameters){ return str_replace(...); //replace placeholders with the values you want });

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, I knew I must be missing something :) Turns out that is in the docs, it's just not named in an obvious way haha. Thanks a lot. I'll accept the answer shortly, once I've jumped back on to that project and tested it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.