Checkboxes and lists with form #states API.

31 января 2023 в 0:05
Аватар пользователя ady1503 ady1503 0 17

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],
  ],
];

}

Комментарии

Why the same field in your example in two cases uses different array depth and keys?

$form['field_operacion_filtro_mantenimi']['#states'] - work well.
$form['field_operacion_filtro_mantenimi']['widget']['#states'] - doesn't work.

So may be both cases should use the same keys as in first case?

31 января 2023 в 2:36

ady1503 wrote: Any ideas?

Try remove ['widget'] in second case. So $form['field_operacion_filtro_mantenimi']['widget']['#states'] becoms to $form['field_operacion_filtro_mantenimi']['#states'] as in the first (working) case.

31 января 2023 в 12:01

Hi @ady1503, can you answer why you asking in English on Russian language forum?
If you know about drupal.ru maybe you know Russian so let's write in Russian,
or if you are not Russian speaking man, from my point of view,
It would be easier for you to ask in any English Drupal forum.
I am just interesting please don't be offended.
Good luck in your job and feel free in our Drupal mega professinal forum!

1 февраля 2023 в 0:12

Привет.

Я пишу на английском, потому что меня попросил модератор в первом комментарии здесь.

Я очень хорошо знаю русский язык.

И да, я присутствую и на форуме drupal.org и поэтому могу и нахожу решения своих вопросов и сомнений, и помогаю другим, чем могу.

У меня нет проблем с общением на русском языке.

Всего наилучшего.

1 февраля 2023 в 0:23

Странно конечно, непонятно почему на английском тебя попросили написать,
наверно на эту аудиторию хотят раскрутиться для поисковиков )))

1 февраля 2023 в 0:26

Я попросил писать на английском, потому что его первые сообщения были похожи на гугл транслейт. Товарищ из Испании, насколько я понял

Не ищи скрытого смысла там, где его нет)

1 февраля 2023 в 6:17

Да, я поэтому и попросил английский, потому что понимать было лучше. Если вас это оскорбляет, пишите по русски, я не возражаю

1 февраля 2023 в 9:36