我正在尝试使用表单类型和'choice_list'为选择选项设置自定义值。
类型:
->add('status', 'choice', array(
'constraints' => array(
new Assert\NotBlank(array('message' => 'Required field missing: status'))
),
'error_bubbling' => true,
'choice_list' => new StatusChoiceList()
StatusChoiceList文件:
class StatusChoiceList extends LazyChoiceList
{
/**
* Loads the choice list
*
* Should be implemented by child classes.
*
* @return ChoiceListInterface The loaded choice list
*/
protected function loadChoiceList()
{
$array = array(
'Preview' => 'Preview',
'Hidden' => 'Hidden',
'Live' => 'Live'
);
$choices = new ChoiceList($array, $array);
return $choices;
}
}
select标签的值为0,1,2,标签为
答案 0 :(得分:3)
ChoiceList类用于选择任意数据类型。在您的情况下,您应该使用SimpleChoiceList代替。第一个参数是一个数组,其中选项为键,标签为值。
protected function loadChoiceList()
{
$array = array(
'Preview' => 'Preview',
'Hidden' => 'Hidden',
'Live' => 'Live'
);
$choices = new SimpleChoiceList($array);
return $choices;
}