2

How do i programatically reset the values of an AJAX submitted form for Drupal 8? I Want to reset them with an AJAX callback. Here is my current callback. It works because i'm forcing the values to NULL.

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

          $form_state->clearErrors();
          $form_state->setRebuild(TRUE);

          $form['address']['#value']    = NULL;
          $form['customer']['#value']   = NULL;
          $form['name']['#value']       = NULL;
          $form['email']['#value']      = NULL;
          $form['location']['#value']   = NULL;
          $form['zip_code_1']['#value'] = NULL;
          $form['zip_code_2']['#value'] = NULL;
          $form['phone']['#value']      = NULL;
          $form['subject']['#value']    = NULL;
          $form['message']['#value']    = NULL;

          return $form;
        }
2
  • $form_state->setUserInput([]); $form_state->setRebuild();
    – user21641
    Commented Jul 12, 2017 at 17:33
  • That doesn't cleans my input values. Do notice i do return $form at the end of my callback. Commented Jul 12, 2017 at 17:36

1 Answer 1

0

I found a couple solutions that works in Drupal 9 that should also work in 8. Untested.

In my buildForm() I am setting some '#default_valueswith$form_state->getValue('name')`. Maybe you are too?

In my case I have a non ajax version of the form that does not have a clear button. In my ajax form's buildForm() function; I simply add "unset()" to the beginning of the form because there is never an instance in which the form needs to be pre-populated on page load in my case. The form will not reload in-between state changes either, so we never have to worry about `#default_value' being set.

Example:

public function buildForm(array $form, FormStateInterface $form_state) {
    //get parent's form
    $form = parent::buildForm($form, $form_state);
    //unset default values so the form clears properly
    unset($form['address']['#default_value']);
    unset($form['customer']['#default_value']);
    unset($form['name']['#default_value']);
    // the rest of your form modifications

    return $form
}

Alternatively or in addition to, if you need your form to be pre-populated on page load via arguments (might be overkill); In your ajax buildForm() you can detect the last callback made and preform your unsets within:

//within the buildForm() ajax function, example clear button with your callback name
$form['clear'] = [
      '#type' => 'button',
      '#value' => $this->t('Clear'),
      '#ajax' => [
        'callback' => '::resetFormCallback',
        'event' => 'click',
      ],

//Check the operating button that submitted a callback action that rebuilt the form
//assign boolean
$form_cleared = 'Clear' === (string) $form_state->getValue('op');
if($form_cleared) {
  //preform unsets here
}

Your Answer

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

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