Hello. A question. I am adding to a field entity reference field, select type, the required option, is dependent on another field, using form api state. And he doesn't react. It is the latest version of drupal. Has something changed? In the other text type field it works. I have tried everything exposed on the Internet and it does not become a required field. And very important is an entity reference field. Thanks for the help.
Form states api.
Главные вкладки
Лучший ответ
Here is the solution, thanks to: wombatbuddy.
https://www.drupal.org/forum/support/module-development-and-code-questio...
Комментарии
Hi,
Perhaps the Conditional Felds module will help you.
If you need to implement this logic in your own code, then show how you do it now.
* con esta funcion controlamos la visibilidad y requerimiento de los campos: field_entidad_externa_limpieza_d, field_datos_de_la_entidad_limpie
* mediante la selecion del campo: field_protocolo_aplicado_por_lim
*/
if (isset($form["field_protocolo_aplicado_por_lim"]) && isset($form["field_entidad_externa_limpieza_d"]) && isset($form["field_datos_de_la_entidad_limpie"])) {
$form['field_entidad_externa_limpieza_d']['#states'] = [
'visible' => [':input[name^="field_protocolo_aplicado_por_lim"]' => ['value' => 'Entidad externa']],
];
$form['field_datos_de_la_entidad_limpie']['#states'] = [
'visible' => [':input[name^="field_protocolo_aplicado_por_lim"]' => ['value' => 'Entidad externa']],
];
$form['field_entidad_externa_limpieza_d']['widget']['0']['target_id']['#states'] = [
'required' => [':input[name^="field_protocolo_aplicado_por_lim"]' => ['value' => 'Entidad externa']],
];
$form['field_datos_de_la_entidad_limpie']['widget']['0']['value']['#states'] = [
'required' => [':input[name^="field_protocolo_aplicado_por_lim"]' => ['value' => 'Entidad externa']],
];
}
/*
* fin
*/
Here https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields,
The State 'required'
The state 'required' is a special case. You can't use 'required' with the field containers, instead you'll have to target the actual input field. Thanks to @tresti88 for his examples in the comments.
Example code by @tresti88. from the comments for the API docs.
$form['auto_complete_field_0']['widget']['0']['target_id']['#states'] = [
'required' => [
[':input[name="my_select_list"]' => ['value' => 'user']],
],
];
Example.
This field is works fine, is simple text field, and his transforms to require.
field_datos_de_la_entidad_limpie
Thanks
If the field_protocolo_aplicado_por_lim field is a entity reference, then in the state conditions you need to specify not the title of the entity, but its ID
When I change to autocomplete widget:
$form['field_entidad_externa_limpieza_d']['widget']['0']['target_id']['#states'] = [
'required' => [':input[name^="field_protocolo_aplicado_por_lim"]' => ['value' => 'Entidad externa']],
];;
Everything works well.
When I transform to select widget the $form['field_entidad_externa_limpieza_d'], it doesn't work.
The problem is in the select widget.
The asterisk that is required appears, but can be saved, without the required field message appearing.
I noticed that the: aria-invalid="true" is not added to $form['field_entidad_externa_limpieza_d'] field, as in another entity reference field that the requirement is activated by default.
It all consists of the select widget.
Thanks if any idea.
Do I understand correctly that the form or the select field_protocolo_aplicado_por_lim is reloaded by ajax?
No, is simple select with two select lines.
This field $form['field_entidad_externa_limpieza_d']['widget']['0']['target_id']['#states'] = [
'required' => [':input[name^="field_protocolo_aplicado_por_lim"]' => ['value' => 'Entidad externa']],
];
is with I don't understand way for work like select box.
What is need for change here ['widget']['0']['target_id']['#states'] for select box?
Thanks
Try 'required' => [':select[name^=...
instead
'required' => [':input[name^=
No, not is solution.
What output do you get in browser console when you run commands
jQuery('select[name^="field_protocolo_aplicado_por_lim"]').val()
and
jQuery('input[name^="field_protocolo_aplicado_por_lim"]').val()
if any value is selected in the control?
Nathing work.
I will look for other solution.
Thanks
I have understood in the end what the problem consists of.
The reference field type, not being predetermined required add by default the option _none as default value.
My function is correct but it does not work for the reference field fields with the Widget Select.
I could not find the solution to control the mandatory requirement of a reference Field, with API #state form.
Can you show the HTML markup of the form?
Here is the solution, thanks to: wombatbuddy.
https://www.drupal.org/forum/support/module-development-and-code-questio...