mcfedr / json-form
A couple of helper files for handling json with symfony forms
Installs: 87 262
Dependents: 1
Suggesters: 0
Security: 0
Stars: 13
Watchers: 4
Forks: 7
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=8.0
- psr/container: ^1.0
- sensio/framework-extra-bundle: ^5.0|^6.0
- symfony/form: ^5.0|^6.0
- symfony/framework-bundle: ^5.0|^6.0
- symfony/validator: ^5.0|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0|^3.0
- phpstan/phpstan-doctrine: ^0.12
- phpstan/phpstan-phpunit: ^0.12
- phpstan/phpstan-symfony: ^0.12
- phpunit/phpunit: ^9
- symfony/browser-kit: ^5.0|^6.0
- symfony/monolog-bundle: ^3.0|^4.0
- symfony/phpunit-bridge: ^5.0|^6.0
- symfony/yaml: ^5.0|^6.0
README
Simply use the JsonControllerTrait
and then use forms as you would normally,
but they now expect to receive JSON.
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 getBlockPrefix() { return 'account'; } }
Then the JSON should be
{ "account": { "name": "Fred" } }
Example
class AccountController extends AbstractController use JsonControllerTrait; /** * @Route("/accounts", methods={"POST"}) */ public function accountCreateAction(Request $request, $uuid) { $account = new Account(); $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