How to add css class to delete button of symfony2 twif template?

I am new to Symfony. My Symfony version is 2.5.7. I created a CRUD using " generate:doctrine:crud

".

Each entry has a delete button in its view. I want to add a button inside a css class.

For this button, the generated code is -

{{ form(delete_form) }} 

      

Upon checking in the browser, I got -

<button type="submit" id="form_submit" name="form[submit]">Delete</button>

      

I changed the generated twig code to -

{{ form_widget(delete_form, {'attr': {'class': 'btn btn-danger btn-lg'}}) }}

      

Upon checking in the browser, I got -

<div id="form" class="btn btn-danger btn-lg">
    <div>
        <button type="submit" id="form_submit" name="form[submit]">Delete</button>
    </div>
    <input type="hidden" id="form__token" name="form[_token]" value="JrBKKEdf1F8hBDYIzu0TP9a4fauKvmlPMGg3rHwwA3w">
 </div>

      

So the button looks like, now -

enter image description here

But I want it to be -

enter image description here

So my generated code should look like this:

<div>
    <button type="submit" id="form_submit" name="form[submit]" class="btn btn-danger btn-lg">Delete</button>
</div>

      

I tried it but it doesn't work. It just shows the normal delete button as the class is not included -

{{ form_widget(delete_form.submit, { 'label': 'Delete' }, { 'attr': {'class': 'btn btn-danger btn-lg', 'type': 'button'} }) }}

      

By the way, without the following button to delete the twig code, the entry is not deleted -

{{ form(delete_form) }}

      

How can I do this with twig ???

+3


source to share


4 answers


I solved the problem .. :) but not from the branch .. from the controller. I added a css class to the method createDeleteForm()

...

private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('book_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete', 
                                            'attr' => array('class' => 'btn btn-danger btn-lg')))
            ->getForm()
        ;
    }

      



But I think this is not good practice. :(

+3


source


Uninstall { 'label': 'Delete' }

and it should work. Here's an example:



{{ form_widget(delete_form.submit, { 'attr': {'class': 'btn btn-danger btn-lg', 'type': 'button'} }) }}

      

+2


source


Expanding on Zed's answer , but I also need a shortcut in another language, all without touching anything but the TWIG template:

   {{ form_widget(delete_form.submit, {'label': 'UsuΕ„', 'attr': {'class': 'btn btn-danger btn-lg', 'type': 'button', 'title': 'DELETE'} }) }}    

      

Later on the same page the "Update by default" button changed:

{{ form_widget(edit_form.submit, {'label': 'Zapisz', 'attr': {'class': 'btn btn-save  btn-lg', 'type': 'button', 'title': 'UPDATE'} }) }} 

      

0


source


All the answers here work to add a class to the submit button. However, if you just use this line with delete_form.submit, the form will not work. Full answer:

{{ form_start(delete_form) }}
{{ form_widget(delete_form.submit, { 'attr': {'class': 'btn btn-danger', 'type': 'button'} }) }}
{{ form_end(delete_form) }}

      

As the doc explains, form_widget does not render the start and end tags of the form:

{# renders all fields #}
{{ form_widget(form) }}

{# renders all fields *and* the form start and end tags #}
{{ form(form) }}

      

0


source







All Articles