Skip to content

Commit

Permalink
update document (syntax highlighting)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 12, 2014
1 parent d03589c commit 6ce21be
Showing 1 changed file with 143 additions and 118 deletions.
261 changes: 143 additions & 118 deletions README.md
Expand Up @@ -29,179 +29,204 @@ Using this bundle you gain new form type options. The "form" field is overwritte

This bundle defines a new service to serialize forms inside the Symfony DIC:

<?php
class UserController extends Controller
```php

class UserController extends Controller
{
public function showAction(User $user)
{
public function showAction(User $user)
{
$serializer = $this->get('form_serializer');
$xml = $serializer->serialize($user, new UserType(), 'xml');
$serializer = $this->get('form_serializer');
$xml = $serializer->serialize($user, new UserType(), 'xml');

return new Response($xml, 200, array('Content-Type' => 'text/xml'));
}
return new Response($xml, 200, array('Content-Type' => 'text/xml'));
}
}

```

The API for the FormListener is documented on the `FormSerializerInterface` it implements:

interface FormSerializerInterface
{
/**
* Serialize a list of objects, where each element is serialized based on a
* form type.
*
* @param array|Traversable $list
* @param FormTypeInterface $type
* @param string $format
* @param string $xmlRootName
*
* @return string
*/
public function serializeList($list, $type, $format, $xmlRootName = 'entries');

/**
* Serialize an object based on a form type, form builder or form instance.
*
* @param mixed $object
* @param FormTypeInterface|FormBuilderInterface|FormInterface $typeBuilder
* @param string $format
*
* @return string
*/
public function serialize($object, $typeBuilder, $format);
}
```php

interface FormSerializerInterface
{
/**
* Serialize a list of objects, where each element is serialized based on a
* form type.
*
* @param array|Traversable $list
* @param FormTypeInterface $type
* @param string $format
* @param string $xmlRootName
*
* @return string
*/
public function serializeList($list, $type, $format, $xmlRootName = 'entries');

/**
* Serialize an object based on a form type, form builder or form instance.
*
* @param mixed $object
* @param FormTypeInterface|FormBuilderInterface|FormInterface $typeBuilder
* @param string $format
*
* @return string
*/
public function serialize($object, $typeBuilder, $format);
}

```

The bundle also registers a Listener inside the form framework that binds XML and JSON requests
onto a form. Just call `$form->bind($request)` as shown in the example below.

If you want to convert JMS Serializer based configuration to FormTypes you can use the command that is included:

php app/console simplethings:convert-jms-metadata "className"
```
php app/console simplethings:convert-jms-metadata "className"
```

Since JMS Serializer automatically builds metadata for every class, you can use this command to generate form types for any existing class for you.

## Configuration

Default DIC (config.yml) configuration:

simple_things_form_serializer:
include_root_in_json: false
application_xml_root_name: ~
naming_strategy: camel_case
encoders:
xml: true
json: true
```yml

simple_things_form_serializer:
include_root_in_json: false
application_xml_root_name: ~
naming_strategy: camel_case
encoders:
xml: true
json: true

```

Dependency Injection tag named `simple_things_form_serializer.encoder` to add more encoders.

## Example

Take a usual form, extended with some details about serialization:

<?php
namespace Acme\DemoBundle\Form\Type;
```php

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
namespace Acme\DemoBundle\Form\Type;

class UserType extends AbstractType
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', 'text')
->add('email', 'email')
->add('country', 'entity')
->add('addresses', 'collection', array('type' => 'address', 'serialize_xml_name' => 'address'))
->add('created', 'datetime', array('read_only' => true))
;
}
$builder
->add('username', 'text')
->add('email', 'email')
->add('country', 'entity')
->add('addresses', 'collection', array('type' => 'address', 'serialize_xml_name' => 'address'))
->add('created', 'datetime', array('read_only' => true))
;
}

public function getName()
{
return 'user';
}
public function getName()
{
return 'user';
}

public function setDefaultOptions(OptionsResolverInterface $options)
{
$options->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\User',
'serialize_xml_name' => 'user',
));
}
public function setDefaultOptions(OptionsResolverInterface $options)
{
$options->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\User',
'serialize_xml_name' => 'user',
));
}
}

```

Using the serializer:

<?php
$serializer = $this->get('form_serializer');
$data = $serializer->serialize($user, new UserType(), 'xml');
```php
$serializer = $this->get('form_serializer');
$data = $serializer->serialize($user, new UserType(), 'xml');
```

Produces:

<user>
<username>beberlei</username>
<email>kontakt@beberlei.de</email>
<country>de</country>
<addresses>
<address>
<street>Foostreet 1</street>
</address>
</addresses>
<created>2012-07-10</created>
</user>
```xml
<user>
<username>beberlei</username>
<email>kontakt@beberlei.de</email>
<country>de</country>
<addresses>
<address>
<street>Foostreet 1</street>
</address>
</addresses>
<created>2012-07-10</created>
</user>
```

Or if you use JSON:

{
"user": {
"username": "beberlei",
"email": "kontakt@beberlei.de",
"country": "de",
"addresses": [
{"street": "Foostreet 1"}
],
"created": "2012-07-10"
}
```json
{
"user": {
"username": "beberlei",
"email": "kontakt@beberlei.de",
"country": "de",
"addresses": [
{"street": "Foostreet 1"}
],
"created": "2012-07-10"
}
}
```

Deserializing will look familiar:

class UserController extends Controller
```php

class UserController extends Controller
{
/**
* @Method("POST")
*/
public function editAction(Request $request)
{
/**
* @Method("POST")
*/
public function editAction(Request $request)
{
$em = $this->get('doctrine.orm.default_entity_manager');
$em = $this->get('doctrine.orm.default_entity_manager');

$user = $em->find('Acme\DemoBundle\Entity\User', $request->get('id'));
$form = $this->createForm(new UserType(), $user);
$user = $em->find('Acme\DemoBundle\Entity\User', $request->get('id'));
$form = $this->createForm(new UserType(), $user);

$form->bind($request);
$form->bind($request);

if ( ! $form->isValid()) {
return $this->renderFormFailure("MyBundle:User:edit.html.twig", $form, array('user' => $user));
}
if ( ! $form->isValid()) {
return $this->renderFormFailure("MyBundle:User:edit.html.twig", $form, array('user' => $user));
}

// do some business logic here
// do some business logic here

$em->flush();
$em->flush();

return $this->formRedirect($form, $this->generateUrl('user_show', array('id' => $user->getId()), 201);
}
return $this->formRedirect($form, $this->generateUrl('user_show', array('id' => $user->getId()), 201);
}

/* either render the form errors as xml/json or the html form again based on " _format" */
public function renderFormFailure($template, FormInterface $form, $parameters)
{
}
/* either render the form errors as xml/json or the html form again based on " _format" */
public function renderFormFailure($template, FormInterface $form, $parameters)
{
}

/* redirect OR 201 created, based on the "_format" */
public function formRedirect()
{
}
/* redirect OR 201 created, based on the "_format" */
public function formRedirect()
{
}
}

```

This looks almost like a out of the book form request. The only thing different
is that we have to use the "renderFormView" and "formRedirect" methods to generate
Expand Down

0 comments on commit 6ce21be

Please sign in to comment.