trevorg
876
25
General

[L5] Where to add custom validation function?

Posted 4 years ago by trevorg

I'm using the new FormRequest functionality, and it works great, but I'm wondering, what is the best way to add a custom validation function?

I would like to have a custom validatePhone() function that validates a phone number.

And then in my EditAccountRequest class I can use it in the rules:

 public function rules()
    {
        return [
            'phone' => ['sometimes','required','phone']
        ];
    }

But I'm not sure where to put the validatePhone() function, and how to tie it into the Validator instance.

I thought maybe doing it this way would work:

$this->getValidatorInstance()->addExtension('phone',...));

But I can't figure it out. Has anyone else successfully done this with Laravel 5?

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

Reply to

Use Markdown with GitHub-flavored code blocks.