Slexy.org is shutting down and stopped accepting new Pastes on May 4th, 2021.
Existing Pastes will stop being available on or after May 10th, 2021.
Author: Roman Yakimin Language: php
Description: Code of my test form Timestamp: 2017-08-28 05:56:06 +0000
View raw paste Reply
namespace Drupal\palom_places\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;


class TestForm extends FormBase {

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

        $form['counter'] = [
            '#type' => 'markup',
            '#markup' => $form_state->getValue('elem') ?? 1,
        ];

        $form['btn'] = [
            '#type' => 'submit',
            '#value' => 'Btn',
            '#ajax' => [
                'callback' => '::addMore',
            ]
        ];

        $i = $form_state->get('elem') ? $form_state->get('elem') : 1;
        $trigger = $form_state->getTriggeringElement();

        if (isset($trigger['#ajax']['callback']) && $trigger['#ajax']['callback'] === '::addMore') {
            $i++;
        }
        $form_state->set('elem', $i);

        return $form;
    }

    public function getFormId(){
        return 'test_form';
    }

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

    }

    public function addMore(array &$form, FormStateInterface $form_state){
        return $form;
    }
}
View raw paste Reply