Skip to content

Instantly share code, notes, and snippets.

@fain182
Created August 19, 2012 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fain182/3394880 to your computer and use it in GitHub Desktop.
Save fain182/3394880 to your computer and use it in GitHub Desktop.
Workaround for Issue #2059 in Symfony 2.0
<?php
namespace Rousseau\PaymentBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
/*
Usage:
$builder->add(
$builder->create('amount')->appendClientTransformer(new CommaToDotTransformer())
);
*/
/* Allow using comma, when only dot is accepted by locale as decimal separator */
class CommaToDotTransformer implements DataTransformerInterface
{
public function transform($number)
{
return $number;
}
public function reverseTransform($input)
{
return str_replace(',', '.', $input);
}
}
@RodolVelasco
Copy link

Im trying to implement this part of your code

My form

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\DataTransformerInterface;
use daci\contratosBundle\Form\Type\CommaToDotTransformer;

class contratosType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
                        ....

            //->add('monto','integer')
            //->add('monto', 'text')
            /*->add('monto', 'number', array(
                'grouping' => true,
            ))*/

                           ..............
            ->add(
                             $builder->create('monto')->appendClientTransformer(new CommaToDotTransformer())
            )
            //->($builder->create('amount')->appendClientTransformer(new CommaToDotTransformer())
            //->add('monto')->appendClientTransformer(new CommaToDotTransformer())
            /*
            ->add('proveedor', 'entity', array(
                'class' => 'daciproveedorBundle:proveedor',
                'property' => 'nombre',
                'empty_value' => 'Elija una opción... ',
                'expanded' => false,
                'multiple' => false,
            ))
            */
            ->add('proveedor', 'genemu_jqueryselect2_entity', array(
                'class' => 'daciproveedorBundle:proveedor',
                'property' => 'nombre',
                'empty_value' => 'Seleccione un proveedor',
                'attr'        => array(
                        'class' => 'contacto_selector',
                        'style' => 'width: 550px;'
                    ),
            ));

And the transformer class

<?php
namespace daci\contratosBundle\Form\Type;

use Symfony\Component\Form\DataTransformerInterface;
//use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;

/*
 Usage:
   $builder->add(
                $builder->create('amount')->appendClientTransformer(new CommaToDotTransformer())
            );
*/

/* Allow using comma, when only dot is accepted by locale as decimal separator */
class CommaToDotTransformer implements DataTransformerInterface
{
    public function transform($number)
    {
        return $number;
    }

    public function reverseTransform($input)
    {
        return str_replace(',', '.', $input);
    }
}

This is my error

FatalErrorException: Error: Call to undefined method Symfony\Component\Form\FormBuilder::appendClientTransformer() in C:\Dropbox\Apache Xampp\evaluacion_daci\src\daci\contratosBundle\Form\Type\contratosType.php line 32

Any idea what is going on

@gaea44
Copy link

gaea44 commented Feb 20, 2015

From symfony 2.1, appendClientTransformer is replaced by addViewTransformer

->add(
    $builder
        ->create('your_field', 'text')
        ->addViewTransformer(new CommaToDotTransformer())
)

@ianitsky
Copy link

Very thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment