0

I built a form based on that documentation page: https://api.drupal.org/api/examples/form_api_example%21src%21Form%21AjaxAddMore.php/class/AjaxAddMore/8.x-1.x

of course I extended that form because I would like to do sections like "accordions" and under that sections I wanna to insert separate fields but in one form. To sum up, every section should has separate fields but it should be in one form because after fill fields I want to submit all fields as one form.

here is my function with buildForm

public function buildForm(array $form, FormStateInterface $form_state) {

$this->renderBusinessValues($form,$form_state);
$this->renderWhatsNew($form,$form_state);

// Group submit handlers in an actions element with a key of "actions" so
// that it gets styled correctly, and so that other modules may add actions
// to the form. This is not required, but is convention.
$form['actions'] = [
  '#type' => 'actions',
];
// Add a submit button that handles the submission of the form.
$form['actions']['submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
];

$form_state->setCached(FALSE);

return $form;

}

and here are functions with building form.

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

    $name_field = $form_state->get('num_names');
    $add_button = $name_field + 1;

    $form_state->set('num_names', $add_button);

    $form_state->setRebuild(true);
  }

  public function addOneWhatsNew(array &$form, FormStateInterface $form_state) {
    $name_field = $form_state->get('num_whats_new');
    $add_button = $name_field + 1;

    $form_state->set('num_whats_new', $add_button);
    $form_state->setRebuild(true);
  }

  /**
   * Submit handler for the "remove one" button.
   *
   * Decrements the max counter and causes a form rebuild.
   */
  public function removeBusinessValue(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);
    }
    $form_state->setRebuild();
  }

  public function removeWhatsNew(array &$form, FormStateInterface $form_state) {
    $name_field = $form_state->get('num_whats_new');
    if ($name_field > 1) {
      $remove_button = $name_field - 1;
      $form_state->set('num_whats_new', $remove_button);
    }
    $form_state->setRebuild();
  }

  public function addmoreWhatsNew(array &$form, FormStateInterface $form_state) {
    return $form['whats_new_fieldset'];
  }

  public function addmoreBusinessValue(array &$form, FormStateInterface $form_state) {
    return $form['business_value_fieldset'];
  }

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

    /**
     * What's new SECTION
     */
    $name_field = $form_state->get('num_whats_new');

    $form['whats_new_header'] = [
      '#type' => 'item',
      '#markup' =>
        '<div class="accordion">
                        <span class="title">What\'s new</span>
                        <span class="spark-arrow-chevron-down"></span>
                    </div>'
    ];
    $form['whats_new_fieldset'] = [
      '#type' => 'fieldset',
      '#attributes' => [
        'class'  => 'panel',
      ],
      '#prefix' => '<div id="whats_new-fieldset-wrapper">',
      '#suffix' => '</div>'
    ];


    $form['whats_new_fieldset']['header'] = [
      '#type' => 'item',
      '#markup' => '<p> test test</p>',
    ];

    if (empty($name_field)) {
      $form_state->set('num_whats_new', 1);
      $name_field = 1;
    }
    for ($i = 0; $i < $name_field; $i++) {
      $form['whats_new_fieldset']['name'][$i] = [
        '#type' => 'textfield',
        '#name' => 'whats_new[]',
        '#title' => 'What\'s new',
      ];
    }

    $form['whats_new_fieldset']['actions'] = [
      '#type' => 'actions',
    ];
    $form['whats_new_fieldset']['actions']['add_names'] = [
      '#type' => 'submit',
      '#value' => t('Add one more'),
      '#field_name' => 'xczx',
      '#submit' => array('::addOneWhatsNew'),
      '#ajax' => [
        'callback' => '::addmoreWhatsNew',
        'wrapper' => 'whats_new-fieldset-wrapper',
      ],
    ];

    // If there is more than one name, add the remove button.
    if ($name_field > 1) {
      $form['whats_new_fieldset']['actions']['remove_names'] = [
        '#type' => 'submit',
        '#value' => $this->t('Remove one'),
        '#submit' => ['::removeWhatsNew'],
        '#ajax' => [
          'callback' => '::addmoreWhatsNew',
          'wrapper' => 'whats_new-fieldset-wrapper',
        ],
      ];
    }
  }

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

    $name_field = $form_state->get('num_names');


    $form['business_value_header'] = [
      '#type' => 'item',
      '#markup' =>
        '<div class="accordion">
                        <span class="title">Business Value</span>
                        <span class="spark-arrow-chevron-down"></span>
                    </div>'
    ];

    $form['business_value_fieldset'] = [
      '#type' => 'container',
      '#attributes' => [
        'class'  => 'panel',
      ],
      '#prefix' => '<div id="business_value-fieldset-wrapper">',
      '#suffix' => '</div>'
    ];


    $form['business_value_fieldset']['header'] = [
      '#type' => 'item',
      '#markup' => '<p>test test</p>',
    ];

    if (empty($name_field)) {
      $form_state->set('num_names', 1);
      $name_field = 1;
    }
    for ($i = 0; $i < $name_field; $i++) {
      $form['business_value_fieldset']['name'][$i] = [
        '#type' => 'textfield',
        '#name' => 'business_value[]',
        '#title' => 'Business Value',
      ];
    }

    $form['business_value_fieldset']['actions'] = [
      '#type' => 'actions',
    ];
    $form['business_value_fieldset']['actions']['add_names'] = [
      '#type' => 'submit',
      '#value' => t('Add one more'),
      '#submit' => array('::addOneBusinessValue'),
      '#ajax' => [
        'callback' => '::addmoreBusinessValue',
        'wrapper' => 'business_value-fieldset-wrapper',
      ],
    ];

    // If there is more than one name, add the remove button.
    if ($name_field > 1) {
      $form['business_value_fieldset']['actions']['remove_names'] = [
        '#type' => 'submit',
        '#value' => $this->t('Remove one'),
        '#submit' => ['::removeBusinessValue'],
        '#ajax' => [
          'callback' => '::addmoreBusinessValue',
          'wrapper' => 'business_value-fieldset-wrapper',
        ],
      ];
    }
  }

My problem occurs after click 'Add one more' on the first section. Every field from the second one overriding values of fields from first section. I don't know what is the problem. Do you have any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.