Symfony2 Forms BooleanToStringTransformer Issue

↘锁芯ラ 提交于 2019-12-10 16:43:19

问题


I have a boolean field that I've put in a form as a choice field (yes or no). I would get 0 or 1 without data transformer. I added a view BooleanToStringTransformer (which seemed reasonable) :

$builder
        ->add(
            $builder->create('myBooleanField', 'choice', array(
                'choices' => array(true => 'Yes', false => 'No'),
            ))
            ->addViewTransformer(new BooleanToStringTransformer('1'))
        )

And when I try to display the form, I get the error "Expected a Boolean.". My field is set to false before creating the form though.

I tried to set it as a model transformer: the form is dispayed, but when I submit it my field is declared invalid.

What am I doing wrong?

Edit: I nearly got it now.

  • I used a model transformer instead of a view transformer (the choice field works with strings or integers, not booleans)
  • I changed my choice list from array(true => 'Yes', false => 'No') to array('yes' => 'Yes', 'no' => 'No')

So the code now looks like ->addModelTransformer(new BooleanToStringTransformer('yes'))

Data transformation works, except that my field is always set to true, whatever value I choose.

What's wrong?


回答1:


The answer is: I shouldn't have thought the default Symfony BooleanToStringDataTransformer was doing the job. It returns null for a false value instead of a string.

So I created my own datatransformer:

<?php

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class BooleanToStringTransformer implements DataTransformerInterface
{
    private $trueValue;
    private $falseValue;

    public function __construct($trueValue, $falseValue)
    {
        $this->trueValue = $trueValue;
        $this->falseValue = $falseValue;
    }

    public function transform($value)
    {
        if (null === $value) {
             return null;
        }

        if (!is_bool($value)) {
            throw new TransformationFailedException('Expected a Boolean.');
        }

        return true === $value ? $this->trueValue : $this->falseValue;
    }

    public function reverseTransform($value)
    {
        if (null === $value) {
            return null;
        }

        if (!is_string($value)) {
            throw new TransformationFailedException('Expected a string.');
        }

        return $this->trueValue === $value;
    }
}



回答2:


You seem to have used a View transformer instead of a Model transformer. You'll need to reverse-transform 0/1 to boolean in the Model transformer if the underlying model expects boolean values.

.. or you might have missed to implement the reverse-transform method in your view transformer.

Read more about the difference between View and Model transformers here.




回答3:


Another workaround can be:

->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    $data = $event->getData();

    if (isset($data['myBooleanField'])) {
        $data['myBooleanField'] = (bool) $data['myBooleanField'];

        $event->setData($data);
    }
})


来源:https://stackoverflow.com/questions/18528318/symfony2-forms-booleantostringtransformer-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!