Score:0

How to save a ajaxified value in a form

gb flag

I add a ajax callback to a select field

$form['field_equipe_membre']['widget']['#ajax'] = array(
'callback' => 'ajax_desactive_unites_field_callback',
'event' => 'change',
'method' => 'html',
'wrapper' => 'edit-field-unite-de-recherche-wrapper',
);

From the triggering_element field, I succeeded to retrieve a value via a db request and select it in another select field

$trigger = $form_state->getTriggeringElement();
$selected_equipe = $trigger['#value'];
 
$query....

$response = new AjaxResponse();
$content = '<option value=\"'.$row_unite['nid'].'\" selected=\"selected\">'.$row_unite['title'].'</option>';
$response->addCommand( new AppendCommand('#edit-field-unite-de-recherche', $content));
return $response;

But when I submit the form, the chosen value by the callback is not saved.

What is the magic lines or the right method to do this?

I tested

$form_state->setRebuild();

in the callback ; I also tested

$form_state->setValue('field_unite_de_recherche', $row_unite['nid']);
$form_state->setCached(TRUE);

I tested the solution proposed somewhere in this forum to define a hidden field in hook_form_edit_alter

$form['product_id']['#type']='hidden';
$form['product_id']['#prefix']='<div id="liberte_pid">';
$form['product_id']['#suffix']='</div>';

and save the value in the callback

$form['product_id']['#value'] = array(
  $row_unite['nid'],
);

But none of these solutions gave the solution for me.

I also red the https://www.drupal.org/project/drupal/issues/2263569 page, but I do not know what to do with.

So : how to save this chosen value by a ajax callback?

Jaypan avatar
de flag
In Drupal, you cannot add form elements in an Ajax Callback - they will be ignored by the system. They have to be added in the form definition, which is cached BEFORE the Ajax callback. See this tutorial the other day that shows an example of how do to this: https://www.jaypan.com/tutorial/drupal-form-api-ajax-form-and-results-same-page
cocq avatar
gb flag
In my case, I did not add form elements in my ajax callback, I select the right value in an existing form element from a db request, itself from a user request. I carefully red your link (and I tried to adapt my code from it), but it is coded from a programming built of the form. In my case, it is an interactive bulding of the form. So, in your example, I cannot manage the "return $form['search_results'];" in the "ajaxSubmit" function. In fact, I suspect that ajax management can only be done from a programming building of a form. I will test this later. So, thanks for the link.
Jaypan avatar
de flag
I can see you adding an `<option>` tag in your ajax callback, so you are adding elements in your ajax callback. You can't add anything to the ajax callback, the form is already cached at that point.
cocq avatar
gb flag
Sorry, but I did not add a value, it is a selection of the right value in the select list. But considering that "the form is already cached at that point", it is normal that update the selected element is useless!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.