StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
792 stars 61 forks source link

enhancement: Add regex parameter #145

Open Androlax2 opened 2 years ago

Androlax2 commented 2 years ago

Hello,

I came with an idea, maybe it could be great to have something like that :

->addText('start_time', [
        'required'    => true,
        'placeholder' => '00:00:00',
        'regex' => '/^(((([0-1][0-9])|(2[0-3])):?[0-5][0-9]:?[0-5][0-9]+$))/'
])

And in the back it would do something like that :

add_filter(
    'acf/validate_value/name=start_time',
    function($valid, $value, $field, $input_name) {
       if (!$valid) {
        return $valid;
    }

    $regex = '/^(((([0-1][0-9])|(2[0-3])):?[0-5][0-9]:?[0-5][0-9]+$))/';
    if (!preg_match($regex, $value)) {
        $valid = 'Please enter a string as follows: hh:mm:ss.';
    }

    return $valid;
    },
    10,
    4
);

Don't know if that would be a good idea. Tell me.