0

I have created a custom form, and I'm passing data to twig file from submitForm function as follows

public function submitForm(array &$form, FormStateInterface $form_state)
{
$form_state->setRebuild();

$data = ['role' => $form_state->getValue('role'),
'date' => $form_state->getValue('date')
];
MyController::Detail($data);

}

and in controller passing data to twig

public static function Detail($data) {

return [
'#theme' => 'my_report',
'#data' => $data,
];

}

but not able to get/print data in twig file.

0

When you set rebuild in a form submit there is no controller involved. The next page displayed is the rebuilt form, where you can print the my_report twig template by adding it to the build array $form.

You don't need to transfer the values in $form_state, because they are available while rebuilding the form. You can check in the form build function whether these values are present. If not, you are building a new form. If they are, this is a form rebuild, then display the result template:

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

    // build form

    if ($form_state->hasValue('role')) {
      $form['my_report'] = [
        '#theme' => 'my_report',
        '#role' => $form_state->getValue('role'),
      ];
    }
    return $form;
  }
  • did you mean to call twig template in submitForm function? because I need to pass submitted data to twig – mintra Jun 3 at 6:25
  • No, I mean the form build function. I add a code example. – 4k4 Jun 3 at 6:36
  • I have added $form['#theme'] = 'my_report'; in build function but it gives empty form means without fields, my requirement is to pass submitted data to twig – mintra Jun 3 at 6:41
  • Sorry, I can't help you any further, because we are not allowed to extend the topic in the comments. My answer is for $form_state->setRebuild();. You can also use $form_state->setRedirect('route.to.controller');. Search for similar questions. If you can't find anything helpful ask a new question. – 4k4 Jun 3 at 16:33
  • I m able to resolve the issue – mintra Jun 3 at 22:26

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.