Multiple fields that are generated dynamically in forms In a particular section of my form, some of fields are iterated over n number of times whenever we click add button n times.

I want to add below details of each student by click add button and remove the details by remove button.

Help would be greatly appreciated :)
Drupal version  is 8.

  $form['reg']= array(
          '#type' =&gt; 'textfield',          
          '#title' =&gt; 'Registration Number',
          '#validated' =&gt; TRUE,
      );
 $form['fname']= array(
          '#type' =&gt; 'textfield',          
          '#title' =&gt; 'First Name',
          '#validated' =&gt; TRUE,
 );
  $form['lname']= array(
          '#type' =&gt; 'textfield',          
          '#title' =&gt; 'last Name',
          '#validated' =&gt; TRUE,
  );
 $form['email'] = array(
       '#type' =&gt; 'textfield',
       '#title' =&gt; 'Email',
       '#validated' =&gt; TRUE
 );
You can use session to store your data like:

$session = new \Symfony\Component\HttpFoundation\Session\Session();
$session-&gt;set('counter', $session-&gt;get('counter',0) + 1);
$counter =$session-&gt;get('counter');

$form['counter'] = array(
   '#type' =&gt; 'hidden',
   '#value' =&gt; $counter
);


Tuto : http://www.drupal8.ovh/en/tutoriels/25/save-user-temporary-data-on-session