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
Comment #2
bluegeek9 commentedI 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.
Comment #3
bluegeek9 commentedComment #4
bluegeek9 commentedIf 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:
Conceptually:
So the rule becomes:
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.