Sam Young
Sam Young

Reputation: 183

Symfony, twig forms, submitting through html form action path

I was wondering is there a way I can submit into the database by using a path that will link to an action in a controller? I have my form in my shoe.html.twig template:

{ % if shoe is defined % }
<form action="{{ path('update_shoe', { 'shoeid' : shoe.id }) }}" method="POST">
   <input type="text" name="color" value="{{shoe.color}}"><br />
   <input type="text" name="brand" value="{{shoe.brand}}"><br />
   <input type="text" name="heel" value="{{shoe.heel}}"><br />
   <button type="submit" name="submit">Update</button>
</form>
{ % endif % }

And the path goes to the controller in this way:

/**
* @Route("/updateshoes/{shoe_id}", name="update_shoe")
* @Method("PUT")
* @Template()
*/
public function updateShoeAction($shoeid) {
      //find the shoe by its id
      //update into database
}

Upvotes: 1

Views: 14619

Answers (1)

Thomas Potaire
Thomas Potaire

Reputation: 6276

It seems that you are on the right track but the only problem with your code is the definition of your route.

Instead of

/**
 * @Method("PUT")
 */

It should be

/**
 * @Method("POST")
 */

Because your form is going to POST to the specified URL.

Otherwise, I'd advise you to use the form component because it will provide you with some nice tools like form validation and entity associations among other things.

Upvotes: 1

Related Questions