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()) {
            return;
        }
        $builder->saveForm();
}

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...

Thanks!

ryanthompson  —  6 years ago

Checkout how this is done here in the users module as an example: https://github.com/anomalylabs/users-module/blob/2.4/src/User/Register/RegisterFormBuilder.php#L50-L61

Note the message is set to false because the validator set's it's own messages on the form here: https://github.com/anomalylabs/users-module/blob/2.4/src/User/Validation/ValidatePassword.php#L30

The UserPassword is a utility for testing passwords against requirements.