How to validate a Mask TextInput?

Currently using this:
TextInput::make('mobile_phone')
->label('Mobile Phone')
->required()
->mask(fn (Mask $mask) => $mask->pattern('(000) 000-0000'))
->tel()
->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'),
TextInput::make('mobile_phone')
->label('Mobile Phone')
->required()
->mask(fn (Mask $mask) => $mask->pattern('(000) 000-0000'))
->tel()
->telRegex('/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/'),
What I want is that if I hit submit, the error should display "The phone number is not valid. Please enter a valid phone number".
4 Replies
Andrew Wallo
Andrew Wallo2y ago
I would look up custom fail logic in the validation section of the Filament form’s documentation. I think it’s near the end of that section.
Mauricio G
Mauricio G11mo ago
Did you manage to solve it?
Andrew Wallo
Andrew Wallo11mo ago
I'm not sure why you were angry about my response to this. You may use the following for this. Maybe this would help you more.
TextInput::make('mobile_phone')
->label('Mobile Phone')
->mask('(999) 999-9999')
->validationMessages([
'regex' => 'The phone number is not valid. Please enter a valid phone number.',
])
->tel()
->telRegex('/^\(\d{3}\) \d{3}-\d{4}$/')
->required(),
TextInput::make('mobile_phone')
->label('Mobile Phone')
->mask('(999) 999-9999')
->validationMessages([
'regex' => 'The phone number is not valid. Please enter a valid phone number.',
])
->tel()
->telRegex('/^\(\d{3}\) \d{3}-\d{4}$/')
->required(),
Mauricio G
Mauricio G10mo ago
Do you want to be my best friend, just kidding, thank you very much
Want results from more Discord servers?
Add your server