I have created a module in which I create a form and then rendered it in twig template I want to know how to give form action in a twig template i.e how to pass form action attribute in twig form.

any help will be greatly appreciated .

mymodulename-form.html.twig file

  <div class="form-group">
   <label for="name">{{form.first_name['#title']}}</label>
    <div class="input-group">
        <span class="input-group-addon danger"><span class="glyphicon icon-remove"></span></span>

I am assuming you are building this form with Form API and not plain HTML (don't do that). You need to add the attributes to ensure you get everything you need.

<form{{ attributes }}>

See the main form.html.twig template: https://api.drupal.org/api/drupal/core%21modules%21system%21templates%21form.html.twig/8.4.x

In general, I advise you to theme the form elements themselves instead of stitch the form together like this, because you will likely be missing the things in the form Drupal adds behind the scenes to a rendered form.

| improve this answer | |
  • hi kevin yes i built this form with form api .then i rendered it in twig file and it got rendered i want to know where to send this form . what to pass in <form > tag . which attributes . the link you mentioned i didn't understand from there. please explain by an example if you can. Thanks – gulshan Dec 4 '17 at 8:35

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.