1

I have see some post about that,but i still have no real answer.

In my form, user can select a register address in a select field. When he select an address, the ajax set value on some other fields :

enter image description here

The code :

  if($form_state->getTriggeringElement()['#name']=="mes_adresses")
  {
      $listAdresse = $this->adresse_service->getUserAdresseById($form_state->getValue('mes_adresses'));

  }


  $form['containerGeneral']['livraison']['adresseComplete']['nom_livraison'] = array(
      '#title' => 'Nom de livraison',
      '#type' => 'textfield',
      '#value' => $listAdresse['nom'],
  );

  $form['containerGeneral']['livraison']['adresseComplete']['prenom_livraison'] = array(
      '#title' => 'Prénom de livraison',
      '#type' => 'textfield',
      '#value' => $listAdresse['prenom'],
  );



  $form['containerGeneral']['livraison']['adresseComplete']['pays_livraison'] = array(
      '#title' => 'Pays de livraison',
      '#type' => 'select',
      '#required'=>true,
      '#options'=>$this->taxonomy_service->getTermsByVid('pays_livraison'),      
      '#value' => $listAdresse['pays'],
  );

  $form['containerGeneral']['livraison']['adresseComplete']['adresse_livraison'] = array(
      '#title' => 'Adresse de livraison',
      '#type' => 'textfield',
      '#value' =>  $listAdresse['adresse'],
  );

  $form['containerGeneral']['livraison']['adresseComplete']['adresse2_livraison'] = array(
      '#title' => 'Adresse de livraison',
      '#type' => 'textfield',
      '#value' =>$listAdresse['adresse2'],
  );

  $form['containerGeneral']['livraison']['adresseComplete']['cp_livraison'] = array(
      '#title' => 'Code postal de livraison',
      '#maxlength'=>5,
      '#type' => 'textfield',
      '#value' => $listAdresse['code_postal'],
  );

  $form['containerGeneral']['livraison']['adresseComplete']['ville_livraison'] = array(
      '#title' => 'Ville de livraison',
      '#type' => 'textfield',
      '#value' =>$listAdresse['ville'],
  );

And the callback :

public function ajaxChangeAdresseCallback(array &$form, FormStateInterface $form_state)
{

    $form_state->setRebuild(TRUE);
    return  $form['containerGeneral']['livraison']['adresseComplete'];

}

So there are 2 things :

#value

If you set value like that, user can never change the value because

$form_state->getValue()

ll keep the value set by the ajax.

#default_value

If you set the value like that, that doesn't work because the #default_value parameter if use just the first time the form is loaded.

The main problem is with $form_state.

If you kint when you submit your form $form_state->getValue() and $_POST, $_POST is good but not the $form_state.

But the issue are in the validate function too, if user select the option new address, ajax reload the form, set empty everywhere, and when you submit, the validate function ll tell you you have empty value.

I know on D7 some people unset the $form_state['values];

But on D8 ?

So how reset the $form_state value ?

2

1 Answer 1

0

Thank to @4k4 for the solution posted here :

How do I change the default value of a field with AJAX?

For my problem, this is how i add this feature :

$form['containerGeneral']['livraison']['adresseComplete'] = array(
          '#type' => 'container',
          '#prefix'=>'  <div id="adresseComplete">',
          '#suffix'=>'</div>'
      );

      $form['containerGeneral']['livraison']['adresseComplete']['nom_livraison']['#tree']=true;
      $form['containerGeneral']['livraison']['adresseComplete']['prenom_livraison']['#tree']=true;
      $form['containerGeneral']['livraison']['adresseComplete']['pays_livraison']['#tree']=true;
      $form['containerGeneral']['livraison']['adresseComplete']['adresse_livraison']['#tree']=true;
      $form['containerGeneral']['livraison']['adresseComplete']['adresse2_livraison']['#tree']=true;
      $form['containerGeneral']['livraison']['adresseComplete']['cp_livraison']['#tree']=true;
      $form['containerGeneral']['livraison']['adresseComplete']['ville_livraison']['#tree']=true;

      if($form_state->getTriggeringElement()['#name']=="mes_adresses")
      {

          $_SESSION['compteur']++;

          $listAdresse = $this->adresse_service->getUserAdresseById($form_state->getValue('mes_adresses'));

          $key = $_SESSION['compteur'];


      }


      $form['containerGeneral']['livraison']['adresseComplete']['nom_livraison'][$key] = array(
          '#title' => 'Nom de livraison',
          '#type' => 'textfield',
          '#required'=>true,
          '#placeholder' => 'Nom',
          '#default_value' => $listAdresse['nom'],
      );

      $form['containerGeneral']['livraison']['adresseComplete']['prenom_livraison'][$key] = array(
          '#title' => 'Prénom de livraison',
          '#type' => 'textfield',
          '#required'=>true,
          '#default_value' => $listAdresse['prenom'],
      );



      $form['containerGeneral']['livraison']['adresseComplete']['pays_livraison'][$key] = array(
          '#title' => 'Pays de livraison',
          '#type' => 'select',
          '#options'=>$this->taxonomy_service->getTermsByVid('pays_livraison'),
          '#default_value' =>  $listAdresse['pays_livraison'],
      );

      $form['containerGeneral']['livraison']['adresseComplete']['adresse_livraison'][$key] = array(
          '#title' => 'Adresse de livraison',
          '#type' => 'textfield',
          '#required'=>true,
          '#placeholder' => 'Adresse de livraison',
          '#default_value' =>  $listAdresse['adresse'],
      );

      $form['containerGeneral']['livraison']['adresseComplete']['adresse2_livraison'][$key] = array(
          '#title' => 'Adresse de livraison',
          '#type' => 'textfield',
          '#default_value' =>$listAdresse['adresse2'],
      );

      $form['containerGeneral']['livraison']['adresseComplete']['cp_livraison'][$key] = array(
          '#title' => 'Code postal de livraison',
          '#maxlength'=>5,
          '#type' => 'textfield',
          '#placeholder' => 'Code postal de livraison',
          '#default_value' => $listAdresse['code_postal'],

      );

      $form['containerGeneral']['livraison']['adresseComplete']['ville_livraison'][$key] = array(
          '#title' => 'Ville de livraison',
          '#type' => 'textfield',
          '#placeholder' => 'Ville de livraison',
          '#default_value' =>$listAdresse['ville'],
      );

Don't forget to set $_SESSION['compteur'] to 0 on submit and to set it to 0 on form load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.