Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PUT request not working with Symfony forms and FosRest

I'm trying to write a simple restful controller for user management in Symfony using FosRest and Symfony forms. My application is backed by Amazon DynamoDB, although I don't think it matters.

I have DELETE, GET and POST (new user) all working perfectly.

I've now come to writing the PUT action (edit user) which doesn't seem to work. I've spent ages banging my head against a brick wall and I just can't work it out.

In order to create the PUT, I essentially copied the POST action but modified it to load the old object first.

In the POST, the User object automatically gets populated by the line $form->handleRequest($request);

This doesn't seem to be working in the PUT action, the user object doesn't get populated/modified. I've checked the $_REQUEST array and the data is being submitted. Because of the lack of browser support for PUT, I'm calling the action by doing a POST of the data with the query parameter _method=PUT (which works fine for DELETE, and it is routing to the correct place).

Here is my POST action that works:

public function postUsersAction(Request $request)
{

    $user = new User();
    $user->setTable($this->getTable());

    $formBuilder = $this->createFormBuilder($user, array(
        'validation_groups' => array('registration', '')))
        ->add('username', 'text')
        ->add('password', 'password')
        ->setAction($this->generateUrl('post_users'))
        ->setMethod('POST')
        ->setAttribute('validation_groups', array('registration'));
    $roles = $this->getFlattenedRoles($this->getRoles());

    $formBuilder->add('roles', 'choice', array(
        'choices'   => $roles,
        'multiple'  => true,
        'expanded'  => true
    ));

    $form = $formBuilder->add('save', 'submit')->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $user->save();
        $params = array('user' => $user);
        $view = $this->view($params, 200)
            ->setTemplate("MyRestBundle:User:newconfirm.html.twig");
        return $this->handleView($view);
    }

    $params = array('form' => $form, 'user' => $user);
    $view = $this->view($params, 400)
        ->setTemplate("MyRestBundle:User:new.html.twig");
    return $this->handleView($view);
}

Here is my PUT controller that doesn't:

public function putUserAction($slug, Request $request)
{
    $table = $this->getTable();
    $user = $table->load($slug);

    $formBuilder = $this->createFormBuilder($user)
        ->add('password', 'password')
        ->setAction($this->generateUrl('put_user', array('slug' => $slug, '_method' => 'PUT')))
        ->setMethod('POST');
    $roles = $this->getFlattenedRoles($this->getRoles());

    $formBuilder->add('roles', 'choice', array(
        'choices'   => $roles,
        'multiple'  => true,
        'expanded'  => true
    ));

    $form = $formBuilder->add('save', 'submit')->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $user->save();
        $params = array('user' => $user);
        $view = $this->view($params, 200)
            ->setTemplate("MyRestBundle:User:newconfirm.html.twig");
        return $this->handleView($view);
    }

    $params = array('form' => $form, 'user' => $user);
    $view = $this->view($params, 400)
        ->setTemplate("MyRestBundle:User:new.html.twig");
    return $this->handleView($view);
}

Any help would be greatly appreciated.

like image 328
theandywaite Avatar asked Dec 17 '13 11:12

theandywaite


1 Answers

Okay, after some help in the Symfony IRC channel, it turns out the reason this isn't working is this part of the code:

$formBuilder = $this->createFormBuilder($user)
    ->add('password', 'password')
    ->setAction($this->generateUrl('put_user', array('slug' => $slug, '_method' => 'PUT')))
    ->setMethod('POST');

Since PUT doesn't work in many browsers, I was trying to set the method as POST and pass _method=PUT as a query parameter to override the real HTTP method. It turns out there is no need to do this, and Symfony will handle it all for you. The above code is now just:

$formBuilder = $this->createFormBuilder($user)
    ->add('password', 'password')
    ->setAction($this->generateUrl('put_user', array('slug' => $slug))
    ->setMethod('PUT');

By doing this, Symfony actually renders a POST form with a hidden _method field - there's no need to do anything manually.

like image 120
theandywaite Avatar answered Nov 07 '22 21:11

theandywaite