2

To add a new validation to Laravel I have done this:

made a new file called customValidation.php in app/start/

and then included it in app/start/global.php and tested it with an echo() and it worked. So it is loaded into the application now.

Then I wrote the following code to validate checkboxes in Laravel:

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class customValidate extends Illuminate\Validation\Validator
{
    public function validateCheckbox($attribute, $value, $parameters)
    {
        //if(isset($value)) { return true; } else { return true; }
        echo "this is the: " . $value;
    }
}

/**
 * Resolvers for Custom Validations
 */
Validator::resolver(function($translator, $data, $rules, $message){
 return new customValidate($translator, $data, $rules, $message);   
});

However, in my validation rules, I define :

`array('sex'=>'checkbox')`

But it does not work. Nothing happens. No error is thrown. The application behaves as if it has not executed the function at all. Also when I echo something from within the function,nothing get echoed, another evidence for the function not being called at all.

3
  • Try to dd('stop') inside of validateCheckbox, to debug and see if method gets called. From what I see, it should be registered and work. Commented Jan 27, 2014 at 8:30
  • no it does not get called Commented Jan 27, 2014 at 8:32
  • Great, now we know it does no gets called. Commented Jan 27, 2014 at 8:35

2 Answers 2

5

I would create custom app/validators folder for this then.

1, Create app/validators/CustomValidate.php

<?php

class CustomValidate extends Illuminate\Validation\Validator
{

    public function validateCheckbox($attribute, $value, $parameters)
    {
        echo "this is the: " . $value;
    }

}

2, run php artisan optimize or composer dumpautoload

3, Somewhere register your custom validator. Perhaps add app/validators.php into start/global.php

Validator::resolver(function($translator, $data, $rules, $message){
    return new CustomValidate($translator, $data, $rules, $message);
});

4, Validate

$rules = ['agreed' => 'checkbox'];
$data = ['agreed' => 0];

$v = Validator::make($data, $rules);

dd($v->passes());
Sign up to request clarification or add additional context in comments.

2 Comments

What would cause me to get CustomValidator::setPresenceVerifier undefined method error?
I don't understand when the CustomValidate (or the validateCheckbox function) called. Could you give explanation?
0

When submitting the form, the radios were not set in their initial states, and this made Laravel not to look for the validator of the item. So, it doesn't have any workaround for that. What I did was I simply added an initial state to the radios and they worked.

Much Like said by Andreyco

1 Comment

Yeah, I always add "empty" value for radios and checkboxes using Form::hidden('checkbox-name', false)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.