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.