- πͺπΈSpain ady1503
Hello.
I'm asking for the community's help again because I think there is a bug in drupal core with the form #state API.
Specifically, checkboxes and lists are not activated as required by form #state.
In the case of checkboxes, the requirement asterisk is not added, but required="required" and aria-required="true" attribute are added, but the checkboxes do not become required.
The same is with the lists except that the asterisk is added, but it is not required either.
I have a list, which I use with the checkbox/radio widget, which is the master.
And I also use the other slave list with the checkbox/radio widget, I have controlled the visibility and the requirement with form #state.
When I choose a value or checkbox from the master list the slave list becomes visible and should be and required.
And the requirement doesn't work, an visibility working well.
I read a lot about this bug, since the drupal 7 version it is not well fixed.
If someone can give me some solution.
Thanks for help.
/** * Implements hook_form_BASE_FORM_ID_alter() for node form. */ function buangh_mantenimiento_diario_piscina_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id): void { $node = $form_state->getFormObject()->getEntity(); if ($node->getType() !== 'mantenimiento_diario_piscina') { return; } // field hide/view $form['field_operacion_filtro_mantenimi']['#states'] = [ 'visible' => [ ':input[name^="field_operacion_de_mantenimiento[Limpieza del filtro]"]' => ['checked' => TRUE], ], ]; // field required $form['field_operacion_filtro_mantenimi']['widget']['#states'] = [ 'required' => [ ':input[name^="field_operacion_de_mantenimiento[Limpieza del filtro]"]' => ['checked' => TRUE], ], ]; }
- Status changed to Needs work
over 2 years ago 12:45am 31 January 2023 - πΊπΈUnited States smustgrave
For #26.
To see if a better solution can be had.
- π¬π§United Kingdom joachim
Re #26, this doesn't seem to be a problem at the checkbox level. This form works and passes validation:
$form['test-3293609'] = [ '#type' => 'checkbox', '#title' => 'test 3293609', '#default_value' => TRUE, '#disabled' => TRUE, '#required' => TRUE, ]; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), ];
This one with a checkboxes element fails validation:
$form['test-3293609'] = [ '#type' => 'checkboxes', '#title' => 'test 3293609', '#options' => [ 'alpha' => 'Alpha', 'beta' => 'Beta', ], '#default_value' => [ 'alpha' => 'alpha', ], '#required' => TRUE, ]; $form['test-3293609']['alpha']['#disabled'] = TRUE; $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), ];