本文介绍了Symfony2形式BooleanToStringTransformer问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔型字段,我把它作为一个选择字段(是或否)。
我会得到0或1没有数据转换器。
我添加了一个视图BooleanToStringTransformer(这看起来很合理):

$ $ p $ $ $ $ $ $ b $> add>
$ builder-> create('myBooleanField','choice',array(
'choices'=> array(true =>'是',false =>'否'),
))
- > addViewTransformer(new BooleanToStringTransformer('1'))

当我尝试显示表单时,出现错误Expected a Boolean。。
在创建表单之前,我的字段设置为false。



我试图将其设置为模型转换器:表单已显示,但是当我提交时我的领域被宣布为无效。



我做错了什么?



编辑:我现在差不多了。




  • 我使用了模型转换器而不是视图转换器(选择字段用于字符串或整数,而不是布尔值)

  • 我从数组中改变了我的选择列表(true =>'是',false =>'否')到 array('yes'=>'是','no'=>'否')



所以现在的代码看起来像 - > addModelTransformer(new BooleanToStringTransformer('yes'))



数据转换的作品,除了我的领域总是设置为true,无论我选择什么样的价值。



有什么不对?

解决方案

答案是:我不应该想到def Symfony BooleanToStringDataTransformer正在完成这项工作。它为null返回一个false值而不是一个字符串。

所以我创建了自己的数据转换器:

 <?php 

使用Symfony \Component\Form\DataTransformerInterface;
使用Symfony \ Component \Form\Exception\TransformationFailedException;

类BooleanToStringTransformer实现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。');
}

返回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;
}
}


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?

解决方案

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;
    }
}

这篇关于Symfony2形式BooleanToStringTransformer问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 12:18