Jerome Ryan Villamor
Jerome Ryan Villamor

Reputation: 13

Laravel Custom Validation with Array as Parameter

Newbie here. Can anyone help me about getting the parameter in my custom validation.

Here is my validation rule :

['materials.*.receive_quantity' => 'lessthan:materials.*.quantity']

Here is my custom validation :

Validator::extend('lessthan', function ($attribute, $value, $parameters, $validator) { return $value <= $parameters[0]; });

When i dd($parameters) it return a string 'materials.*.quantity'. TIA.

Upvotes: 0

Views: 1728

Answers (1)

brunen9
brunen9

Reputation: 112

First, you'll need a 5.4.18 release. Then, try this code:

Validator::extendDependent('lessthan', function ($attribute, $value, $parameters, $validator) {
    return $value <= array_get($validator->getData(), $parameters[0]);
});

And you may look at this pull request for more explanations and examples: PR#18564

P.S. And may be this is a typo in your code: your rule name is "lessthan", but you use the "<=" operator instead of "<".

Upvotes: 2

Related Questions