Add custom validation to form builder
Created 6 years ago by emergingdzns

I have a module I built that has a text field. I've added a mask of 00/00/0000 to the field as well as a placeholder example to get users to enter a valid date. This is NOT a datetime field because the client didn't like the picker at all because the dates the user needs to enter are typically years in the past. So I made it text with the mask. However many are still being epically stupid and not entering the dates correctly.

When they enter it wrong it's often d/m/y format or some stupidity like 20/11/0103 (for Jan 3, 2011). So.....

In the form builder handler I have the following:

public function handle(MyFormBuilder $builder) {
        if (!$builder->canSave()) {

So what would be the best way to add a custom validator to this class? Do I just pull in the laravel validator class or is there a nice easy pyro way of saying "check the entry and if it's not a valid date, return an error"? I'm drawing a blank on this...


ryanthompson  —  6 years ago

Checkout how this is done here in the users module as an example:

Note the message is set to false because the validator set's it's own messages on the form here:

The UserPassword is a utility for testing passwords against requirements.