PUT request not working with Symfony forms and FosRest - php

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.

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.

Related

Symfony2 Request-URI Too Long

I have action, but when i send data, i have
Request-URI Too Long. The requested URL's length exceeds the capacity limit for this server
public function addAction(Request $request)
{
$productGallery = new ProductGallery();
$product = new Product();
$productGallery->addProductgalleryToProduct($product);
$form = $this->createForm(new ProductGalleryType(), $productGallery);
if($request->isMethod('POST'))
{
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($productGallery);
$em->persist($product);
$em->flush();
return $this->redirectToRoute('addAction', array('form' => $form->createView()));
}
}
return array(
'form' => $form->createView()
);
}
How i can fixed it? What i do wrong?
p.s my form collection
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('productgallery_to_product', 'collection', array(
'type' => new ProductType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true
))
;
}
NEW INFO
Method 'POST'
In my url
http://trololo.com/app_dev.php/add?form%5Bvars%5D%5Bid%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bname%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bfull_name%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bdisabled%5D=0&form%5Bvars%5D%5Bmultipart%5D=1&form%5Bvars%5D%5Bblock_prefixes%5D%5B0%5D=form&form%5Bvars%5D%5Bblock_prefixes%5D%5B1%5D=games_modelbundle_productgallery&form%5Bvars%5D%5Bblock_prefixes%5D%5B2%5D=_games_modelbundle_productgallery&form%5Bvars%5D%5Bunique_block_prefix%5D=_games_modelbundle_productgallery&form%5Bvars%5D%5Bcache_key%5D=_games_modelbundle_productgallery_games_modelbundle_productgallery&form%5Bvars%5D%5Bread_only%5D=0&form%5Bvars%5D%5Bvalid%5D=1&form%5Bvars%5D%5Brequired%5D=1&form%5Bvars%5D%5Bcompound%5D=1&form%5Bvars%5D%5Bmethod%5D=POST&form%5Bvars%5D%5Baction%5D=&form%5Bvars%5D%5Bsubmitted%5D=1&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bid%5D=games_modelbundle_productgallery_productgallery_to_product&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bname%5D=productgallery_to_product&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bfull_name%5D=games_modelbundle_productgallery%5Bproductgallery_to_product%5D&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bdisabled%5D=0&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bmultipart%5D=1&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bblock_prefixes%5D%5B0%5D=form&form%5Bchildren%5D%5Bproductgallery_to_product%5D%5Bvars%5D%5Bblock_prefixes%5D%5B1%5D=collection&form%5Bchildren%5D%5Bprodu.....
You're passing a whole form view object in the URL:
$this->redirectToRoute('addAction', array('form' => $form->createView()));
The second argument to redirectToRoute() is a list of GET parameters to send with the request.
This makes your URL VERY long. It exceeds the web server limit, which in turn refuses to handle the request.
Your call should look more like this:
$this->redirectToRoute('addAction');
Also, the first argument to the redirectToRoute() method is the route name, not the action method name. Replace it unless your route name is "addAction".
Read more in the Controller chapter of the documentation.

Symfony 2.3 handleRequest($request) doesn't set entity

I have contact Entity, making post call using:
postContactAction(Request $request)
Generated form using app/console generate:doctrine:form BundleName:Contact
In post function trying to get request and set entity to request parameters like name, email and etc:
$contact = new Contact();
$form = $this->createForm(new ContactType(), $contact, array(
'method' => 'POST'));
$form->handleRequest($request);
And while checking, var_dump($contact); returns fields with null value.
What can be the problem?
Using postman to send post request and it worked without form.
In ContactType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('email', 'text', array('required' => false))
->add('phone', 'integer', array('required' => false))
->add('text', 'textarea')
->add('subject', 'text')
->add('createdAt', 'datetime', array('required' => false))
;
}
In ContactController:
public function postContactAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$this->initErrorContainer();
$validator = $this->get('validator');
$contactDetails = $this->getRequest()->request->all();
// check if email and phone exists or not. One is enough
if (!array_key_exists('email', $contactDetails) && !array_key_exists('phone', $contactDetails)) {
$this->errorContainer->createAndAdd('email', $this->errorContainer->MESSAGE_REQUIRED);
$this->errorContainer->createAndAdd('phone', $this->errorContainer->MESSAGE_REQUIRED);
return $this->getView();
}
$contact = new Contact();
// handle form
$form = $this->createForm(new ContactType(), $contact, array(
'method' => 'POST'));
$form->handleRequest($request);
// set default values
var_dump($contact);
die;
Solved. As Entity uses different types than request, you need to create simply model of this entity, set request fields to that entity type using handleRequest, and then you can use it.
Is weird, but can you try to get the form after call createForm to see if that has something related, namely:
you have:
$form = $this->createForm(new ContactType(), $contact, array('method' => 'POST'));
Then just add:
$form = $this->createForm(new ContactType(), $contact, array('method' => 'POST'))->getForm();

Form page after Form page

I am having a problem in my symfony 2 project regarding the use of form (forms without classes). The idea is to develop a sequence of forms (each one in an independent page). Similar to multistep form or wizard but exactly not the same. In each form, partial sent results are saved and a new form is called.
Currently I have a controller with a form like this:
public function form1Action(Request $request, $parameter){
//Get relevant data using the $parameter...
$defaultData = array('message' => 'Type your message here');
$formBuilder = $this->createFormBuilder($defaultData);
$formBuilder->add('sendreport', 'choice', array(
'choices' => array('yes' => 'Yes', 'no' => 'No'),
'expanded' => true,
'label' => 'You will receive a full report from administrator',
'multiple' => false,
));
$formBuilder->add('start', 'submit', array('label' => 'form.labels.start'));
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
if ($form->isSubmitted() && $form->isValid()){
$data = $form->getData();
return $this->redirect($this->generateUrl('task_form2', array('data' => $data)));
}
return $this->render('MyBundle:Default:showform1.html.twig', array('ourform' => $form->createView()));
}
Then, the redirected page contains a new form with similar structure to the previous one:
public function form2Action(Request $request, $data){
$datasource = $data;
$defaultData = array('message' => 'Type your message here');
$formBuilder = $this->createFormBuilder($defaultData)
->add('name', 'text')
->add('email', 'text')
->add('message', 'textarea')
->add('send', 'submit');
$form = $formBuilder->getForm();
$form->handleRequest($request);
//WHY IS NOT SUBMMITED???
if ($request->getMethod() == 'POST') {
if ($form->isSubmitted() /*&& $form2->isValid()*/) {
$data2 = $form->getData();
$data = //$data = $data2 array + $datasource array...
return $this->redirect($this->generateUrl('task_form3'), array('data' => $data));
}
}
return $this->render('MyBundle:Default:showform2.html.twig', array('ourform' => $form->createView()));
}
But here is the problem, the second form called in 'task_form2' path is not submitted!¿?. When you press the send button nothing happens!.
Any idea?
What is the best way to do this? (I think it is simpler and I would not like to use multistep bundle or something like that).
Thank you in advance.

How to retrieve an array of entities from the reques object?

I have an entity field type in my form, but then when I try to get the values from the controller I get an error.
This is my form builder
$builder
->add('recursos', 'entity', array(
'class' => 'SIGIConvocatoriasBundle:Recurso',
'property' => 'nombre',
'multiple' => true,
'mapped' => false
))
->add('requisitos', 'entity', array(
'class' => 'SIGIConvocatoriasBundle:Requisito',
'property' => 'nombre',
'multiple' => true,
'mapped' => false
))
;
and this is my controller
$entity = new Convocatoria();
$form = $this->createForm(new ConvocatoriaType(), $entity);
$form->bind($request);
$recursos = $request->request->get('recursos');
foreach ($recursos as $recurso)
{
//Do something ...
}
But I get an error here
Invalid argument in foreach ...
Like if the $recursos variable is empty or something, and I get a 'recursos' => null in the symfony exception.
I'd really appreciate some help here :D
The request itself contains raw data (scalars).
When you bind the request to the form, it will transform this raw data to normalized data.
The array of ids will be transformed to an array of entities, and then be passed to $entity->setRecursos(); // or each one to $entity->addRecurso();
$form = $this->createForm(new ConvocatoriaType(), $entity)
$form->bind($request);
$formData = $request->request->get($form->getName());
$formData['recursos']; // should be an array of ids
$entity->getRecursos(); // array of entities
Try
$entity = new Convocatoria();
$form = $this->createForm(new ConvocatoriaType(), $entity);
$form->bind($request);
foreach ($entity->getRecursos() as $recurse) {
//do something
}
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->flush();

How to get default value for form field in symfony2 formbuilder

So I have read that I can set the default value of a field in symfony2 formbuilder by doing this
->add('myfield', 'text', array(
'label' => 'Field',
'data' => 'Default value'
))
Now I want to manipulate the value that would be here by default, in order to know what to set, I need to know what was originally passed, so can I get the default value like
myfield.default_value
so that i can later manipulate it and set the result of my manipulation as the real default value?
Addition:
I tried
->add('username', 'text', array('label' => 'Användarnamn ', 'data' => 'my_own_defaultvalue'))
That didn't work for me (seen it in a bunch of forums though...).
Then I realized that probably the easiest way is to just edit the object before I pass it to the formBuilder, like this:
public function editUserAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('BizTVUserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Container entity.');
}
//run some action to modify one of the variables of $entity
$entity->setUsername($username_stripped); //put my edited variable into the object, now we're ready to pass my new and improved $entity to the formBuilder...
$editForm = $this->createForm(new editUserType(), $entity);
return $this->render('BizTVUserBundle:Default:editUser.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}

Categories