Custom validation rule not working

Posted 2 years ago by roemer

Hi, I've written a custom validation rule that checks whether a field is larger than another field. So say, I want to filter by price, so I need price_to to be larger than price_from. The rule is as follows:

        Validator::extend('greater_than_field', function($attribute, $value, $parameters, $validator)
            $minField = $parameters[0];
            $data = $validator->getData();
            $minValue = $data[$minField];
            Log::debug(sprintf('Checking if %d is larger than %d.', $value, $minValue));
            return $value > $minValue;

However, when I try to implement it like so:

        $validator = Validator::make($request->all(), [
            'price_from' => 'required|integer',
            'price_to' => 'required|integer|greater_than_field:price_from',
            'percentage_from' => 'required|integer|min:0',
            'percentage_to' => 'required|integer|greater_than_field:percentage_from|max:100',

        if ($validator->fails())
            return response()->json($validator->messages());

It still passes! And see that little Log::debug() in the custom validation? That doesn't work either. No logs are added to my storage/logs/laravel.log, hence why I'm posting here.

Any ideas? Thanks!

Please sign in or create an account to participate in this conversation.