我有这样的表格:
class CampaignType extends AbstractType {
protected $pricings;
public function __construct(array $pricings){
$this->pricings = $pricings;
}
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->setMethod('POST')
->add('name', 'text')
->add('code', 'text')
->add('pricings', 'choice', array(
'choices' => $this->pricings,
'expanded' => false,
'multiple' => true
))
->add('save', 'submit');
}
}
$ pricing是传递给表单的键值数组。
array(2) {
[2335331]=> string(38) "1 Months - 1 Issues - 34 - Credit card"
[2335332]=> string(40) "12 Months - 12 Issues - 23 - Credit card"
[2335333]=> string(40) "24 Months - 12 Issues - 23 - Credit card"
}
现在,当我需要编辑记录时,我将$ defaultData传递给表单:
array(10) {
["id"]=> string(1) "4"
["subsite_id"]=> string(3) "104"
["name"]=> string(5) "ffsgd"
["code"]=> string(6) "dfgfdg"
["pricings"]=>
array(2) {
[2335331]=> string(38) "1 Months - 1 Issues - 34 - Credit card"
[2335332]=> string(40) "12 Months - 12 Issues - 23 - Credit card" } }
但是多选不会将传递的选项视为已选择。
有没有办法将多选小部件的几个选项标记为已选中,具体取决于从数据库中获取的键值数组?不使用Doctrine
答案 0 :(得分:0)
至于VisioN建议,必须使用'data'选项,只需使用一个简单的选定值数组,而不是我想的键值数组。
所以Form声明看起来像这样:
class CampaignType extends AbstractType {
protected $pricings;
public function __construct(array $pricings, array $selectedPricings){
$this->pricings = $pricings;
$this->selectedPricings;
}
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->setMethod('POST')
->add('name', 'text')
->add('code', 'text')
->add('pricings', 'choice', array(
'choices' => $this->pricings,
'expanded' => false,
'multiple' => true,
'data' => $this->selectedPricing //array('2335331', '2335332')
))
->add('save', 'submit');
}
}