Home » Php » php – Using FOSRestBundle with forms

php – Using FOSRestBundle with forms

Posted by: admin July 12, 2020 Leave a comment

Questions:

Noob, question relating to FOSRestBundle, JMSSerializerBundle and templates.

I’m attempting to replace some existing code that’s currently outputting json via twig to use the FOSRestBundle. This has been successful where content being passed from the Controller was originally in arrays, but now I’m trying to pass a form to FOSRestBundle, the result is my values never get returned.

The code below replicates the scenario

/**
 *  my sample get action
 * @View(templateVar="form")   
 */
public function getAction($id)
{
    ...         
     $form = $this->createFormBuilder(array('myValue' => 'SOMEVALUE'))
        ->add('myValue', 'hidden')
        ->getForm();

    $view = FOSView::create($form);
    $view->setFormat('json');

    return $this->get('fos_rest.view_handler')->handle($view);
}

returns

{"children":{"_token":[],"myValue":[]}}

what I expected to see here was something like:

{"children":{"_token": "mylongtoken","myValue": "SOMEVALUE"}}

I’ve been basing my code on the examples in LiipHelloBundle, unless I’m mistaken this matches the examples they provide? Any ideas where I’m going wrong?

How to&Answers:

OK, not exactly the cleanest solution I would expect within Symfony, but it seems to work so:

$form->createView()->get('form')->get('form')->getChild('myValue')->get('choices')

Returns me the myValue entities:

{“28″:”Default1″,”103″:”test”}