function AjaxAddMore::removeCallback

Same name in other branches
  1. 3.x modules/form_api_example/src/Form/AjaxAddMore.php \Drupal\form_api_example\Form\AjaxAddMore::removeCallback()
  2. 4.0.x modules/form_api_example/src/Form/AjaxAddMore.php \Drupal\form_api_example\Form\AjaxAddMore::removeCallback()

Submit handler for the "remove one" button.

Decrements the max counter and causes a form rebuild.

File

form_api_example/src/Form/AjaxAddMore.php, line 121

Class

AjaxAddMore
Implements the ajax demo form controller.

Namespace

Drupal\form_api_example\Form

Code

public function removeCallback(array &$form, FormStateInterface $form_state) {
    $name_field = $form_state->get('num_names');
    if ($name_field > 1) {
        $remove_button = $name_field - 1;
        $form_state->set('num_names', $remove_button);
    }
    // Since our buildForm() method relies on the value of 'num_names' to
    // generate 'name' form elements, we have to tell the form to rebuild. If we
    // don't do this, the form builder will not call buildForm().
    $form_state->setRebuild();
}