Form API Validation: Add New Filter to convert input values into machine-readable names

Created on 24 January 2025, 4 days ago

Problem/Motivation

Currently, the Form API Validation module does not provide a built-in filter to automatically convert input values into machine-readable names. This can lead to issues where user input intended for use as identifiers contains spaces, special characters, or uppercase letters, making it unsuitable for use as machine names without manual processing.
Adding a filter for machine_name would allow developers to automatically sanitize and convert input values into machine-readable names, ensuring consistency and reducing the risk of errors in systems that rely on machine names.

Proposed resolution

Introduce a new filter option for Form API Validation to convert input values into machine-readable names. This filter will:
Automatically convert user input into lowercase, replace spaces with underscores or hyphens, and remove special characters.
Be configurable at the form element level, similar to existing filters like trim.
Example usage in a form definition:

$form['example_textfield'] = [
  '#type' => 'textfield',
  '#title' => t('Example Field'),
  '#filters' => [
    'machine_name' => [],
  ],
];
✨ Feature request
Status

Active

Version

3.0

Component

Code

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024