2014-06-09 21 views
0

我有这样的形式:Symfony2的 - 这取决于通过数据来自数据库的选择标记的选择 - 没有教义

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'); 
} 

}

$定价是一个关键值数组传递到窗体。

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" } } 

但多选择的选择没有得到过的选项。

是否有任何方法可以将多选部件的几个选项标记为选中状态,具体取决于从数据库中获取的键值数组?没有使用原则

+1

传递所选值的简单数组到'了'choice'领域的data'选项。 – VisioN

回答

0

至于VisioN建议,必须使用“数据”选项,只需使用一组简单的选定值,而不是像我想的那样的键值数组。

所以窗体声明是这样的:

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'); 
    } 
}