Support the ongoing development of Laravel.io →
posted 9 years ago
Validation
Last updated 2 years ago.
0

Is it possible to use $parameters? i think all passed parameters sent are in that?

Last updated 2 years ago.
0

That's what I thought too, but $parameters[0] and $parameters[1] only returns the name of the fields =/

Last updated 2 years ago.
0

I mean, those are the parameters from the validation file, but I thought It was translated to its values somehow.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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 :)

Last updated 2 years ago.
0

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;
        });        
    }
0

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;
});    
Last updated 8 years ago.
0

Thanks @bluedot74 for the update!

Works like a charm!

0

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.

0

@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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Cederman cederman Joined 22 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.