Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite form submit button id?

Tags:

php

twig

symfony

I'm using the attr option to set the custom ID for my deletion form button:

{{ form_widget(form.delete, { 'label': 'myCustomLabel', 'attr': {'id': 'myCustomId'} }) }}

But it works with anything ('class' for example) except the 'id' attribute. The id is still 'form_delete' and I can't change it even with form builder:

$this->createFormBuilder(null, ['csrf_protection' => false])
        ->setAction($this->generateUrl('task_delete', array(
            'prefix' => self::getTaskMapper()::getPrefix($task),
            'id' => $task->getId()
        )))->add('delete', SubmitType::class, [
            'label' => 'delete',
            'attr' => ['id' => 'MyCustomId']
        ])
        ->setMethod('DELETE')
        ->getForm();

Why is that the case? How do I overwrite it?

Form id overwriting works well.

like image 332
Audiophile Avatar asked Sep 09 '26 20:09

Audiophile


1 Answers

Because you shouldn't be using attr. Just set the id on the main form_widget options:

form_widget(form.delete, { 'label': 'myCustomLabel','id': 'myCustomId' }) }}