1 Answer
My proposition is to write custom validation rule, where you will be conditionally check if it is a valid email or phone number.
Validator::extend('email_or_phone', function ($attribute, $value, $parameters, $validator) {
return
$validator->validateDigitsBetween($attribute, $value, [8, 10]) ||
$validator->validateEmail($attribute, $value, []);
});
Write this code in boot
method in for example AppServiceProvider
. I also strongly recommend you to use dedicated classes for custom rules as described here: https://laravel.com/docs/9.x/validation#custom-validation-rules
In both ways you just end up with code like this:
public function rules()
{
return [
'credentials' => ['email_or_phone'], // HERE WE USE OUR CUSTOM RULE
'g-recaptcha-response' => ['required'],
];
}
0 comments:
Post a Comment
Thanks