我尝试通过Sonata Admin Bundle编辑或添加产品,但验证器总是拒绝"condition"字段,因为"您选择的值不是有效的选择"。
管理类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');
}
try this:
//. .
->add('condition', 'entity', array(
'class' => YourAppBundle:YourEntityProduct
'label' => 'Stan',
));
. .//
Doctrine期望获得字符串,但您将整数作为所选字段的值传递给它。
这就是你传递的内容:
return array(
0 => 'new',
1 => 'used'
);
这就是你需要的(例如):
return array(
'0' => 'new',
'1' => 'used'
);
字段长度验证触发错误
Sonata使用值作为标签来显示,并使用键作为值(传递给模型)。要得到你想要的,你的数组应该像array('new' => 'new', 'used' => 'used');