How do I conditionally add a field?
I'm trying to find out any example of adding new conditional field using ajax in Drupal 8 form API, but I can't find it.
This is my code.
$form['name'] = array( '#type' => 'textfield', '#title' => $this->t('Name'), '#maxlength' => 64, '#size' => 64, '#default_value' => $name, ); $form['floor'] = array( '#type' => 'textfield', '#title' => $this->t('Floor'), '#default_value' => $floor, ); $form['flat_per_floor'] = array( '#type' => 'textfield', '#title' => $this->t('Flat Per Floor'), '#default_value' => $flatPerFloor, '#ajax' => array( 'callback' => 'Drupal\bms_flat\Form\FlatForm::flatGenerate', 'effect' => 'fade', 'event' => 'change', 'method' => 'replace', 'progress' => array( 'type' => 'throbber', 'message' => "Generating..", ), ), ); if ($form_values['floor']) { $form['flats'] = array( '#type' => 'checkboxes', '#title' => $this->t('Content Type'), '#options' => node_type_get_names(), ); } $form['submit'] = array( '#type' => 'submit', '#title' => $this->t('Submit'), '#default_value' => $this->t($submit), );This is the callback function.
public function flatGenerate(array &$form, FormStateInterface $form_state) { // … }How do I add a new field after filling-up my Flat Per Floor field?