Sandy
Sandy

Reputation: 210

Laravel 5.0 Custom Validation Method not Found

i have a custom validation rules that works on my dev server but when i push it to my production server, it's become error. It said that Method [validationFoo] not Found. here is my code :

AppServiceProvide.php

public function boot()
{
Validator::extend('is_even_length', function($attribute, $value, $params, $validator){
    return strlen($value)%2==0;
});

Validator::replacer('is_even_length', function($message, $attribute, $rule, $params) {
    return str_replace('_', ' ' , 'The '. $attribute .' must have an even length !!' );
});
}

and here is my controller

  $rules = [
        'test' => 'required|max:16|min:12|is_even_length'
    ];

    $validator = Validator::make(Input::get(),$rules);`

i have include

    use Illuminate\Support\Facades\Input;
    use Illuminate\Support\Facades\Validator;

on my AppServiceProvider.php and my controller

is there any wrong on my code?

Thanks

Upvotes: 1

Views: 1034

Answers (2)

Sandeesh
Sandeesh

Reputation: 11906

Looks like your custom rule isn't getting registered and laravel is defaulting to find the method in it's base class. Try running php artisan clear-compiled. Also you can use use Input; and use Validator; since they have facades registered.

Upvotes: 1

Demonyowh
Demonyowh

Reputation: 1672

this is because is_even_length does not exists in resources/lang/en/validation.php

in your boot

Validator::extend('is_even_length', function($attribute, $value, $parameters,  
validator) {
     if(!empty($value) && (strlen($value) % 2) == 0){
         return true;
     }
     return false;
});

then in resources/lang/en/validation.php add your validation message like

'is_even_length' => "The :attribute must have an even length.",

Upvotes: 1

Related Questions