1

How can I add correctly custom validation rule in Laravel 5?

In Laravel 4 it was placed in some autoloaded file:

Validator::register('alpha_spaces', function($attribute, $value)
{
    return preg_match('/^([-a-z0-9_-\s])+$/i', $value);
});
2
  • 2
    I thought it was "extend" rather than "register"? Maybe I'm remembering incorrectly. Commented Nov 27, 2014 at 20:37
  • 1
    See my response here if you are using FormRequest. Commented Nov 27, 2014 at 20:48

1 Answer 1

3

You can check the documentation: http://laravel.com/docs/5.0/validation#custom-validation-rules

     Validator::extend('foo', function($attribute, $value, $parameters) {
         return $value == 'foo'; 
});

I think you only need to change 'register' with 'extend'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.