Skip to content

Latest commit

 

History

History
81 lines (59 loc) · 2.14 KB

README.md

File metadata and controls

81 lines (59 loc) · 2.14 KB

Json Form helper

Simply extend the JsonController and then use forms as you would normally, but they now expect to receive JSON. Something like this.

Latest Stable Version License Build Status SensioLabsInsight

Install

Composer

php composer.phar require mcfedr/json-form

AppKernel

Include the bundle in your AppKernel

public function registerBundles()
{
    $bundles = array(
        ...
        new Mcfedr\JsonFormBundle\McfedrJsonFormBundle()

JSON

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

Example

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
        ]);
    }
}

Tests

To run the tests

./vendor/bin/phpunit