Is it possible to use $parameters? i think all passed parameters sent are in that?
That's what I thought too, but $parameters[0] and $parameters[1] only returns the name of the fields =/
I mean, those are the parameters from the validation file, but I thought It was translated to its values somehow.
Try this:
$param1 = array_get($this->data, $parameters[0]);
$param2 = array_get($this->data, $parameters[1]);
Remember that if any of the parameters is a file, you should use $this->files instead of $this->data.
Regards,
Alexandre Leites.
Awesome, it works! Somehow I feel that this needs to be mentioned in the docs. Custom validators with comparisons is quite common I would think :)
it doesn't work for me. I have a CustomValidationServiceProvider, that works. But $this->data
is null. Why?
public function boot()
{
Validator::extend('sameAge', function($attribute, $value, $parameters) {
$team_a = Team::findOrFail($value);
$team_b = array_get($this->data, $parameters[0]);
return $team_a->min_age == $team_b->min_age;
});
}
Little late but for future reference, blonkm:
Add a 4th parameter ($validator) to your closure and call getData() on it:
Validator::extend('sameAge', function($attribute, $value, $parameters, $validator) {
$team_a = Team::findOrFail($value);
$team_b = array_get($validator->getData(), $parameters[0]);
return $team_a->min_age == $team_b->min_age;
});
Hi, i´m having a similar problem so i would like to ask you how i can pass arguments from the request rules() to the validator function. i attach the code which is not working.
rules fuction in request:
public function rules()
{
return [
'fechaVal' => 'ultimos_seismeses:18,25', // 18 and 25 are arguments
];
}
validator function:
protected function validateUltimosSeismeses( $attribute, $value, $parameters, $validator) {
$param1 = array_get($validator->getData(), $parameters[0]);
if( $param1 == 18 ) {
return true;
}
else {
return false;
}
}
what´s the problem? I really appreciate any help you can provide.
@MAXI279 I had the same issue, but it looks like (at least with Laravel 5.x) that the $parameters variable is populated exactly how you'd expect - indices and values. So you can now access them with just $parameters[n] or use a foreach loop if you don't have a set number of parameters.
I wrote a quick "Whitelist" validator that could be very beneficial as a part of Laravel's stock validators. My thought is that if we have a select input, and you only have a few valid options, you can use this filter to very easily confirm that the field is an acceptable value. Whether or not it makes it into Laravel, here it is for any interested developers:
Validator::extend('whitelist', function($attribute, $value, $parameters, $validator) {
foreach ($parameters as $whitelist) {
if ($value == $whitelist)
return true;
}
return false;
});
Cheers, hope this helps some fellow devs.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community