Name element should scope required state to minimum components when #states['required'] is applied to the wrapper/element

Created on 15 October 2025, 17 days ago

Problem/Motivation

When a Name field is made required via #states['required'] on the field’s wrapper (e.g., by Conditional Fields), Drupal’s states.js treats all subcomponents as required on the front-end. The UI shows asterisks on non-minimum components, and the browser can block submission on those optional inputs. Server-side validation already correctly respects minimum components; this is a client-side scoping issue.

Steps to reproduce

  1. Create a content type with a Name field; set minimum components to Given + Family only.
  2. Add a checkbox gate; attach #states['required'] to the Name field widget wrapper based on the checkbox.

    You could do something like:

    function my_module_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
      if (!isset($form['field_gate'], $form['field_name'])) {
        return;
      }
    
      // Put the state on the field wrapper.
      $form['field_name']['#states']['required'] = [
        ':input[name="field_gate[value]"]' => ['checked' => TRUE],
      ];
    }
    
  3. Load the form and toggle the checkbox.

Actual: Asterisks/HTML required apply to all components (e.g., Middle, Credentials).

Expected: Only minimum components (e.g., Given, Family) are visually required.

Proposed resolution

Add a small JS behavior to the Name module that listens for the jQuery state:required event on the Name field wrapper/element and re-scopes required UI to minimum components only. This keeps server-side behavior intact and fixes the browser/UI.

Remaining tasks

  • Ship JS behavior + library
  • Add FunctionalJavascript test to verify scoping on toggle and initial load.
  • Attach it when rendering the Name element.
  • Docs note explaining the behavior.

User interface changes

Only the Name field’s minimum components show the required asterisk and HTML required when made required via states.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇺🇸United States jcandan

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Pipeline finished with Failed
    17 days ago
    #629064
  • Pipeline finished with Failed
    15 days ago
    Total: 360s
    #631100
  • Pipeline finished with Failed
    11 days ago
    #635376
  • Pipeline finished with Failed
    11 days ago
    Total: 337s
    #635400
  • Pipeline finished with Success
    11 days ago
    Total: 1024s
    #635475
  • Pipeline finished with Success
    9 days ago
    Total: 301s
    #637545
  • Pipeline finished with Failed
    about 21 hours ago
    Total: 342s
    #645798
Production build 0.71.5 2024