PHP code example of simplethings / form-serializer-bundle
1. Go to this page and download the library: Download simplethings/form-serializer-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
simplethings / form-serializer-bundle example snippets
class UserController extends Controller
{
public function showAction(User $user)
{
$serializer = $this->get('form_serializer');
$xml = $serializer->serialize($user, new UserType(), 'xml');
return new Response($xml, 200, array('Content-Type' => 'text/xml'));
}
}
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);
}
namespace Acme\DemoBundle\Form\Type;
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)
{
$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 setDefaultOptions(OptionsResolverInterface $options)
{
$options->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\User',
'serialize_xml_name' => 'user',
));
}
}
$serializer = $this->get('form_serializer');
$data = $serializer->serialize($user, new UserType(), 'xml');
class UserController extends Controller
{
/**
* @Method("POST")
*/
public function editAction(Request $request)
{
$em = $this->get('doctrine.orm.default_entity_manager');
$user = $em->find('Acme\DemoBundle\Entity\User', $request->get('id'));
$form = $this->createForm(new UserType(), $user);
$form->bind($request);
if ( ! $form->isValid()) {
return $this->renderFormFailure("MyBundle:User:edit.html.twig", $form, array('user' => $user));
}
// do some business logic here
$em->flush();
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)
{
}
/* redirect OR 201 created, based on the "_format" */
public function formRedirect()
{
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.