Attribute identifier on symfony form tag

I would like to define the id attribute in my symfony2 forms.

I tried this in my branch template:

{{ form_start(form, {'id': 'form_person_edit'}) }} 

But it seems unworkable.

+43
symfony twig
Sep 05 '13 at 15:23
source share
3 answers

Have you tried attr ?

 {{ form_start(form, {'attr': {'id': 'form_person_edit'}}) }} 
+82
Sep 05 '13 at 15:30
source share

Enter the identifier in the parameter array, which is passed to the form constructor:

 public function newAction(Request $request) { // create a task and give it some dummy data for this example $task = new Task(); $task->setTask('Write a blog post'); $task->setDueDate(new \DateTime('tomorrow')); $form = $this->createFormBuilder($task, ['attr' => ['id' => 'task-form']]) ->add('task', 'text') ->add('dueDate', 'date') ->add('save', 'submit', ['label' => 'Create Post']) ->getForm(); return $this->render('AcmeTaskBundle:Default:new.html.twig', [ 'form' => $form->createView(), ]); } 

Or in the form of a form:

 class TaskType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('task') ->add('dueDate', null, ['widget' => 'single_text']) ->add('save', 'submit'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults([ 'data_class' => 'Acme\TaskBundle\Entity\Task', 'attr' => ['id' => 'task-form'] ]); } public function getName() { return 'task'; } } 
+19
Aug 11 '14 at 14:21
source share

In addition, I must add to the above answers that you can do this in your controller as follows:

 $this->createForm(FormTypeInterFace, data, options); 

For the sample - I did it like this:

 $this->createForm(registrationType::class, null, array( 'action' => $this->generateUrl('some_route'), 'attr' => array( 'id' => 'some_id', 'class' => 'some_class' ) )); 
+2
Feb 05 '16 at 8:31
source share



All Articles