Skip to content

Repository files navigation

Json Form helper

Simply use the JsonControllerTrait and then use forms as you would normally, but they now expect to receive JSON.

Latest Stable VersionLicenseBuild Status

Install

Composer

php composer.phar require mcfedr/json-form

AppKernel

Include the bundle in your AppKernel

publicfunction registerBundles()
{
$bundles = array(
...
newMcfedr\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
{
publicfunctionbuildForm(FormBuilderInterface$builder, array$options)
{
$builder
->add('name');
}
publicfunctiongetBlockPrefix()
{
return'account';
}
}

Then the JSON should be

{
"account": {
"name": "Fred"
}
}

Example

class AccountController extends AbstractController
use JsonControllerTrait;
/** * @Route("/accounts", methods={"POST"}) */publicfunctionaccountCreateAction(Request$request, $uuid) {
$account = newAccount();
$form = $this->createJsonForm(AccountType::class, $account);
$this->handleJsonForm($form, $request);
$em = $this->getDoctrine()->getManager();
$em->persist($account);
$em->flush();
return$this->json([
'account' => $account
]);
}
}

For Symfony 3.x you will need to extend Controller because the trait needs access to getParameter method.

Contributing

To run the tests

./vendor/bin/php-cs-fixer fix
./vendor/bin/phpunit
./vendor/bin/phpstan analyse

About

Helper to post JSON to Symfony forms

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages