ZaquPL
ZaquPL

Reputation: 799

How to set form name in Symfony2?

I create my custom form in this way:

$messageForm = $this->createFormBuilder( new MessageEmailForm() )
        ->add('name', 'text')
        ->getForm();

I would like to change form name but I don't know how to do it and simultaneously hold MessageEmailForm class in FormBuilder. I tried something like that:

 $this->createFormBuilder( new MessageEmailForm(), "myCustomFormName" )

etc. but it doesn't work. I need exactly this result:

<form name="myCustomFormName" ... >

@EDIT// I found information about my problem, it looks like I should use following code but I have no idea where I should use it...

$this->get('form.factory')->createNamedBuilder

Upvotes: 1

Views: 3939

Answers (2)

Martin Fasani
Martin Fasani

Reputation: 823

If you want to generate this:

<form name="myCustomFormName" ... >

Just add this in the twig template:

{{ form_start(form, {'attr': { 'name':'myCustomFormName' }})  }}

Upvotes: 4

Queli Coto
Queli Coto

Reputation: 696

Could you add the attribute to your twig template.

{# src/Acme/TaskBundle/Resources/views/Default/new.html.twig #}
<form action="{{ path('task_new') }}" name="myCustomFormName" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}

    <input type="submit" />
</form>

Upvotes: 0

Related Questions