Form states api.

Главные вкладки

Аватар пользователя ady1503 ady1503 23 января в 10:36

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.

Лучший ответ

Комментарии

Аватар пользователя ady1503 ady1503 23 января в 19:37
  /*
  * 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

Аватар пользователя ady1503 ady1503 24 января в 1:03

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.

Аватар пользователя ady1503 ady1503 24 января в 9:04

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

Аватар пользователя Andruxa Andruxa 24 января в 19:54

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?

Аватар пользователя ady1503 ady1503 25 января в 1:04

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.