function AjaxAddMore::addOne

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

Submit handler for the "add-one-more" button.

Increments the max counter and causes a rebuild.

File

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

Class

AjaxAddMore
Implements the ajax demo form controller.

Namespace

Drupal\form_api_example\Form

Code

public function addOne(array &$form, FormStateInterface $form_state) {
    $name_field = $form_state->get('num_names');
    $add_button = $name_field + 1;
    $form_state->set('num_names', $add_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();
}