Forms 使用SimpleChicelist时保存值而不是键
这是我的表单的主要功能:Forms 使用SimpleChicelist时保存值而不是键,forms,symfony,symfony-2.4,Forms,Symfony,Symfony 2.4,这是我的表单的主要功能: /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('naam', 'text', array( 'label' => "Naam",
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('naam', 'text', array(
'label' => "Naam",
'attr' => array(
'placeholder' => "Vul hier een functionele omschrijving in van de deltaload",
)
))
->add('tabel', 'choice', array(
'empty_value' => 'Kies een tabel',
'choices' => $this->loadChoiceList()->getChoices()
))
;
}
protected function loadChoiceList()
{
$array = array(
'Preview' => 'Preview',
'Hidden' => 'Hidden',
'Live' => 'Live'
);
$choices = new SimpleChoiceList($array);
return $choices;
}
这是loadChoiceList()函数:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('naam', 'text', array(
'label' => "Naam",
'attr' => array(
'placeholder' => "Vul hier een functionele omschrijving in van de deltaload",
)
))
->add('tabel', 'choice', array(
'empty_value' => 'Kies een tabel',
'choices' => $this->loadChoiceList()->getChoices()
))
;
}
protected function loadChoiceList()
{
$array = array(
'Preview' => 'Preview',
'Hidden' => 'Hidden',
'Live' => 'Live'
);
$choices = new SimpleChoiceList($array);
return $choices;
}
查看此表单时,我可以从三个已定义的选项中选择一个。但在将其保存到数据库时,会根据选择保存0、1或2
如何使其保存值而不是键
我还尝试了ChoiceList()类,但结果没有什么不同
作为记录,我意识到这不是规范化的DB设计,但这是用一个简化的例子来解释我的问题的简化方法。答案很简单
更改此项:
'choices' => $this->loadChoiceList()->getChoices()
致:
或者,如果要使用“choices”选项,可以使用函数返回正则数组,而不是SimpleChicelist对象 太好了,我知道我很接近。谢谢你的回答,托马扎林。