26

Form helpers form_start and form_end are useful in twig:

{{ form_start(form) }}
{{ form_end(form) }}

I can customise some parameters like the method or the action. But I need to customise others parameters like the class or add the form-enctype.

Can I do it? Should i set up it into the FormType.php?

Since now I simply try to add my customised value to the twig function like below:

{{ form_start(form, {'class': 'myclass', 'action': 'myaction'}) }}
// fields...
{{ form_end(form, {'render_rest': true}) }}

But in this case, for example, the class does not appear.

Pmpr
  • 14,501
  • 21
  • 73
  • 91
Roberto Rizzi
  • 1,411
  • 5
  • 22
  • 36

1 Answers1

48

As form_start has the following signature,

form_start(view, variables)

And as class doesn't represent a valid variable name. You need to specify your class as a key/value array using the attr attribute.

Then, try ...

{{ form_start(form, {'action': 'myaction', 'attr': {'class': 'your_class_name'}}) }}

Also ...

  • You should be sure that your Type fileds are well defined in order to let the form_start helper set the right form-enctype.

  • The form_enctype(formView) helper is deprecated since Symfony 2.3.

T0xicCode
  • 3,791
  • 2
  • 27
  • 47
Ahmed Siouani
  • 13,064
  • 11
  • 59
  • 70
  • Thank you very much! This solution works perfectly. Then, i don't have to use the `form-enctype` also whether i have the files upload because the `form_start()` set up automatically the `form-enctype'. That's correct? However into the form tags i don't see it. – Roberto Rizzi Sep 16 '13 at 13:18
  • 5
    Why `class` is duplicated in the `variables` array? – A.L Feb 05 '15 at 23:46
  • 4
    The proposed code snippet is in contrary to your explanation, as you have put `class` as a variable! – Pmpr Nov 09 '16 at 09:13
  • 4
    Should be `{{ form_start(form, {'attr': {'class': 'your-class'} }) }}` – dotoree Jun 07 '18 at 14:53
  • same is true for `id`. It must also go in the `attr` array. – mcmurphy Jul 19 '18 at 20:22