2017 © Pedro Peláez
 

symfony-bundle json-http-form-bundle

Adds support of JSON requests for Forms

image

elao/json-http-form-bundle

Adds support of JSON requests for Forms

  • Wednesday, June 27, 2018
  • by Elao
  • Repository
  • 16 Watchers
  • 29 Stars
  • 2,911 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Elao JSON HTTP Form Bundle

Build Status, (*1)

Adds support of JSON requests for Forms:, (*2)

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

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., (*4)

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., (*5)

Installation

Require ElaoJsonHttpFormBundle:, (*6)

composer require elao/json-http-form-bundle

Usage

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

The following form and controller are meant to create a new instance of Rocket:, (*8)

<?php

namespace AppBundle\Form\Type;

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

// ...

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

    // ...
}
<?php

namespace AppBundle\Controller;

// ...

class 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:, (*9)

POST /rockets HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 43

{"name":"Melies","colors":["pink","brown"]}

It works \o/, (*10)

## License

MIT, (*11)

Author Information

http://www.elao.com/, (*12)

The Versions

27/06 2018

dev-master

9999999-dev https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form

27/06 2018

v2.0.2

2.0.2.0 https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form

01/12 2016

v2.0.1

2.0.1.0 https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form

04/05 2016

v2.0

2.0.0.0 https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form

04/05 2016

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form

05/04 2016

v1.0

1.0.0.0 https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form

20/04 2015

v0.2.0

0.2.0.0 https://github.com/Elao/ElaoJsonHttpFormBundle.git

Adds support of JSON requests for Forms

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest form