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?
Hello.
Mi code is corect.
Here is how to:
https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields
Drupal core have an bug.
Thanks.
Any ideas?
Can you provide some proof link?
https://www.drupal.org/project/drupal/issues/2855139
https://www.drupal.org/project/drupal/issues/2950999
https://www.drupal.org/forum/support/module-development-and-code-questio...
Have you tried this?
PS. Is your second field multiple?
PS. Is your second field multiple? --- yes
and not working an example of link
Seems like this is really core bug.
Another fresh patch for this: https://www.drupal.org/project/drupal/issues/2731991 (#50)
this is for radio I have checkboxes, I try the patch and not working
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.I also check this option. Not working.
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!
Привет.
Я пишу на английском, потому что меня попросил модератор в первом комментарии здесь.
Я очень хорошо знаю русский язык.
И да, я присутствую и на форуме drupal.org и поэтому могу и нахожу решения своих вопросов и сомнений, и помогаю другим, чем могу.
У меня нет проблем с общением на русском языке.
Всего наилучшего.
Странно конечно, непонятно почему на английском тебя попросили написать,
наверно на эту аудиторию хотят раскрутиться для поисковиков )))
Я попросил писать на английском, потому что его первые сообщения были похожи на гугл транслейт. Товарищ из Испании, насколько я понял
Не ищи скрытого смысла там, где его нет)
Добрый.
Не важно на каком языке, важно понять друг друга и сделать лучше Drupal.
Хорошого дня всем.
Да, я поэтому и попросил английский, потому что понимать было лучше. Если вас это оскорбляет, пишите по русски, я не возражаю
А мы-то тут стараемся ))
В целом напомнило: