Elao/ElaoJsonHttpFormBundle

Name: ElaoJsonHttpFormBundle

Owner: Elao

Description: Adds support for JSON POST requests to Symfony Forms

Created: 2015-02-17 15:18:55.0

Updated: 2017-02-08 12:37:03.0

Pushed: 2016-12-01 11:03:25.0

Homepage: null

Size: 21

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Elao JSON HTTP Form Bundle

Build Status

Adds support of JSON requests for Forms:

Symfony forms will be able to handle both JSON POST/PUT/PATCH/DELETE requests and standard GET/POST requests (as they are by default).

The JsonHttpFoundationRequestHandler handles the request: If the request content-type is JSON, it decodes the JSON request content as an array and submits the form with its data.

Otherwise, it lets the default behaviour operate: the HttpFoundationRequestHandler will handle the request. So all your non-json form request will be treated just the way they've always been.

Installation:

Add ElaoJsonHttpFormBundle to your composer.json:

p composer.phar require elao/json-http-form-bundle

Register the bundle in the kernel:

p
pp/AppKernel.php

ic function registerBundles()

$bundles = array(
    // ...
    new Elao\Bundle\JsonHttpFormBundle\ElaoJsonHttpFormBundle(),
);

That's it. You're good. Get some well deserved rest.

Usage:

Given a Rocket entity with two attributes: name (a string) and colors (an array of strings).

The following form and controller are meant to create a new instance of Rocket:

p

space AppBundle\Form\Type;

Symfony\Component\Form\AbstractType;
Symfony\Component\Form\Extension\Core\Type\TextType;
Symfony\Component\Form\Extension\Core\Type\ChoiceType;

..

s RocketType extends AbstractType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class)
        ->add('colors', ChoiceType::class, [
            'multiple' => true,
            'choices'  => [
                'White'  => 'white',
                'Orange' => 'orange',
                'Blonde' => 'blonde',
                'Pink'   => 'pink',
                'Blue'   => 'blue',
                'Brown'  => 'brown',
            ]
        ])
    ;
}

// ...

hp
p

space AppBundle\Controller;

..

s RocketController extends Controller

public function newAction(Request $request)
{
    $rocket = new Rocket();
    $form   = $this->createForm(new RocketType(), $rocket)->getForm();

    if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
        // The $rocket object is now correctly hydrated with the data from the form.
        // Whether the request is a classic GET/POST request or a JSON one.
    }
}

The Controller and Form above now accept the following JSON POST request:

 /rockets HTTP/1.1
pt: application/json
ent-Type: application/json
ent-Length: 43

me":"Melies","colors":["pink","brown"]}

\o/

License

MIT

Author Information

http://www.elao.com/


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.