Sonata Admin Bundle选择验证错误

时间:2015-08-25 15:41:52

标签: php symfony sonata-admin

我尝试通过Sonata Admin Bundle编辑或添加产品,但验证程序始终拒绝“条件”字段,因为“您选择的值不是有效选择。”

管理员课程

protected function configureFormFields(FormMapper $formMapper)
{
        $formMapper
        ->add('name', 'text', array('label' => 'Nazwa'))
        ->add('condition', 'choice', array(
                'choices' => Product::getConditions(), 
                'label' => 'Stan',  
        ));
}

实体

/**
 * @Assert\Choice(callback = "getConditions")
 * @ORM\Column(type="string", length=10)
 */
protected $condition;

public static function getConditions()
{
    return array('new', 'used');
}

3 个答案:

答案 0 :(得分:1)

试试这个:

// ..

FormView could not be converted to string

..//

答案 1 :(得分:1)

Doctrine期望获取字符串,但您将整数作为所选字段的值传递给它 这就是你通过的:

 return array(
    0 => 'new',
    1 => 'used'
);

这就是你所需要的(例如):

 return array(
    '0' => 'new',
    '1' => 'used'
);

错误由字段上的长度验证触发。

答案 2 :(得分:1)

Sonata使用值作为标签来显示,将Keys作为值(传递给模型)。为了得到你想要的你的数组应该像数组('new'=>'new','used'=>'used');