I attempted to remove the 'required' bit from a name field in a form so that the form can be submitted without the a name provided when some other boxes are ticked.

However, I wasn't successful with that. Maybe I'm doing something wrong or perhaps this just doesn't work yet?

I would have expected this to work:

  $form['field_name']['#states'] = [
    'optional' => [
      ':input[name="my_radio_button_field"]' => ['value' => 'my_value'],
    ],
  ];

In fact this does remove the asterisk (*) after the name of the field (although it doesn't come back when the radio button is changed again, which would need fixing as well) but when I hit the submit button, it still asks me to fill in the different components of the name field.

Any ideas where to start?

Comments

PhilippVerpoort created an issue. See original summary.

bluegeek9’s picture

I was not able to not make the name field not required using states.

I was able to make the field required.

Maybe you can change how the checkbox works; make the field optional by default and make it required based on the value of another input.

bluegeek9’s picture

Category: Bug report » Support request
  • Check whether this field is configured as required at field level. If yes, Drupal entity validation will also enforce non-empty.
  • Don’t rely on #states['optional'] alone for this case.
  • Make the field non-required at base form build time, then enforce conditional requirement in custom validation.
  • You may need to remove/replace [Name::class, 'validateElement'] and call equivalent validation only when your radio condition requires it.
bluegeek9’s picture

Component: Code » Documentation
Status: Active » Fixed

If the field is conditionally optional, do not mark the field required in field settings. A required field setting is unconditional server-side/entity validation, and #states['optional'] cannot reliably override that.

Instead:

  1. Set the name field as not required in field settings.
  2. Use #states['required'] on the name field’s actual widget/component inputs for browser-side behavior when the radio value means “name is required”.
  3. Add a custom validation callback that mirrors the same condition server-side.

Conceptually:

if ($radio_value !== 'my_value' && name_is_empty($name_value)) {
  $form_state->setError($form['field_name'], t('Name is required.'));
}

So the rule becomes:

  • Field config: optional by default.
  • #states['required']: UX/browser hint when the condition requires it.
  • Custom validate callback: source of truth on submit.

One caveat for this module: an optional name field can still reject a partial name because #minimum_components validation still applies once any part of the name is filled in. That’s usually desirable: empty is allowed, but if the user starts entering a name, it must be complete enough according to the field’s component settings.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.