Simply extend the JsonController
and then use forms as you would normally, but they now expect to receive JSON.
Something like this.
php composer.phar require mcfedr/json-form
Include the bundle in your AppKernel
public function registerBundles()
{
$bundles = array(
...
new Mcfedr\JsonFormBundle\McfedrJsonFormBundle()
The expected JSON will be just like that form values that would be sent.
Suppose you have the following form type
class AccountType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name');
}
public function getName()
{
return 'account';
}
}
Then the JSON should be
{
"account": {
"name": "Fred"
}
}
class ActionController extends JsonController
/**
* @Route("/actions/{uuid}", requirements={"uuid"="[a-z0-9-]{36}"})
* @Method("POST")
*/
public function actionCreateAction(Request $request, $uuid) {
$action = new Action();
$form = $this->createForm(new ActionType(), $action);
$this->handleJsonForm($form, $request);
$em = $this->getDoctrine()->getManager();
$em->persist($action);
$em->flush();
return new JsonResponse([
'action' => $action
]);
}
}
To run the tests
./vendor/bin/phpunit