helmi
helmi

Reputation: 140

how to call a controller function with a symfony2 form button

Usually i declare my buttons in symfony2 like this:

<a href="{{ path('delete_route_name', {'id': entity.id }) }}">Delete</a>

but in this case i have to use the following statement:

$form->add('submit', 'submit', array('label' => 'Save Changes', 'attr' => array('style' => 'position: relative; left:33%')));

because this button is just in some cases (within the form) necessery.

As well i want call a function in a controller with this button. How i can add a controller path to this button?

of course i can work with JavaScript, but im looking for a Solution without some JavaScript.

Thanks for the feedbacks.

Upvotes: 0

Views: 962

Answers (2)

netsuo
netsuo

Reputation: 109

You can use multiple submit buttons and in your code do something like this:


        if ($form->isSubmitted() && $form->isValid()) {

            //button 1
            if ($form->get('submit_1')->isClicked()) {
                return $this->myOtherControllerAction($request, $form);
                //or $this->forward   see https://symfony.com/doc/current/controller/forwarding.html
            }
        }

Upvotes: 0

user4144415
user4144415

Reputation:

You must add the path to the form action as an form config parameter.

If you want to add a <a href=""></a> link/button to the form, take a look to the docs to learn how to render Symfony forms widget by widget (or row by row). Then, you can add the link/button above the form_end(form) function in your template.

Upvotes: 1

Related Questions