Audiophile Audiophile - 1 year ago 98
PHP Question

How to overwrite form submit button id?

I'm using the

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']

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

Form id overwriting works well.

Answer Source

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' }) }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download