Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Workaround for Issue #2059 in Symfony 2.0

View gist:3394880
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?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);
}
}

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

From symfony 2.1, appendClientTransformer is replaced by addViewTransformer

->add(
    $builder
        ->create('your_field', 'text')
        ->addViewTransformer(new CommaToDotTransformer())
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.