Using mask field in custom module

Created on 30 July 2023, over 1 year ago
Updated 11 August 2023, over 1 year ago

Problem/Motivation

I want to use mask field in a custom module. Which is very straightforward for a textfield as I just need to add a mask attribute as so.

    $form['myelement'] [
      '#type' => 'textfield',
      '#title' => 'My custom field',
      '#size' => 19,
      '#maxlength' => 19,
      '#attributes' => [
        'placeholder' => '0000-0000-0000-0000',
      ]
      '#mask' =>  [
        'value' => '0000-0000-0000-000A',
        'reverse' => false,
        'selectonfocus' => false,
        'clearifnotmatch' => false
      ];

This works beautifully for the preset patterns. However in my case I need a custom pattern 0000-0000-0000-000C where C is a digit from 0-9 or an X. Now I could add that into the presets and it would work just fine, however I want my field to work out of the box if mask field is present.

What would be great if I could add custom patterns in through either a hook (if the mask field module is enabled) or better still via the '#mask' attribute for example:

    $form['myelement'] [
      '#type' => 'textfield',
      '#title' => 'My custom field',
      '#size' => 19,
      '#maxlength' => 19,
      '#attributes' => [
        'placeholder' => '0000-0000-0000-0000',
      ]
      '#mask' =>  [
        'value' => '0000-0000-0000-000A',
        'reverse' => false,
        'selectonfocus' => false,
        'clearifnotmatch' => false,
        'custompatterns' => [
          'C' => [
            'pattern' => '[0-9X]',
          ]
        ]
      ];

i'm aware i could add my own pattern via install but that seems a messy way to do it.

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇬🇧United Kingdom mistergroove

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024