Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FOSRestBundle with forms

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?

like image 913
MadManMonty Avatar asked May 14 '12 21:05

MadManMonty


1 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"}

like image 143
MadManMonty Avatar answered Oct 19 '22 23:10

MadManMonty