1

I don't have much experience in drupal development. So for the purpose of learning, i have made a custom module and trying to implement various functionalities in drupal 8 module development. I want to rebuild a form on submission of the form. I took help from here and here and drupal.org's this node. How can i rebuild the form on clicking on the button 'Click Me'. It is not throwing any error but the form is not getting rebuild in the following piece of code :

function say_hello_submit(&$form, &$form_state) {

    $form_state->setRebuild();
  }
public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('hugs.settings');

    $form['default_count'] = [
        '#type' => 'number',
        '#title' => $this->t('Default_hug_count'),
        '#default_value' => $config->get('default_count'),
    ];
    $form['mytable'] = [
        '#type' => 'table',
        '#header' => [t('Name'), t('Value')],
        '#rows' => array(),
    ];
    for ($i=1; $i<=2; $i++) {
        $form['mytable'][$i]['name'] = array(
            '#type' => 'textfield',
            '#size' => 40,
            '#maxlength' => 40,
            '#title' => t('Name'),
             '#title_display' => 'invisible',
     );

        $form['mytable'][$i]['value'] = array(
            '#type' => 'textfield',
            '#size' => 40,
            '#maxlength' => 40,
            '#title' => t('Value'),
            '#title_display' => 'invisible',
     );
    }
    $form['submit'] = array('#type' => 'submit', '#value' => 'Click Me');
    $form['#submit'][] = 'say_hello_submit' ;

    return parent::buildForm($form, $form_state);

}
2

What exactly do you expect to happen when it's "rebuilding"?

I should simply call buildForm() again, but that will build the same form, so visually, you won't see a difference. The only ondication would be that it's doing only a POST request, while a non-rebuilding form submission does a redirect to the same page after submitting.

If you e.g. want to see additional rows in your list, you need to store the desired max with $form_state->set('max_rows, 5) and then in your buildForm() method, read it again from there with get()

1

I haven't tried this if Drupal 8, but if say_hello_submit is a member of your form class, you'll need to link it to the class instance. This notation works for me for other form callbacks, but if haven't tried it for a submit handler yet:

$form['#submit'][] = array($this, 'say_hello_submit');

Also, the handler will need to be a public function.

2
  • 1
    i made the changes you ave specified, now it shows "The Configuration options have been saved" but the form is not rebuilding Jan 5 '16 at 10:59
  • 1
    You could try like this: $form['#submit'][] = '::say_hello_submit'; Failing that, the only other thing i can think of is to move the function back into the .module file, but that would go against the whole point of having a form class, and I can't believe that would be the intended way. Jan 6 '16 at 13:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.