1

我正在使用选择框处理表单。这些方框代表国家之间不同的比例。

因此,我创建了一个服务,它生成自定义值以根据用户的偏好提供选择框。这适用于第一级表单:

控制器代码:

$form = $this->createForm(new formType, $entity, array(

     // Getting the service    
     'myScales'     => $this->get('myBnd.scales'),

     // Getting user's scale preference
     'scalesLocale'  => $this->get('security.context')->getToken()->getUser()->getScaleLng(),

     ));

然后,我在 formType 中得到了显示自定义选择所需的一切:

public function buildForm(FormBuilder $builder, array $options) {

    $scaleSelect = array();        
    // Here is a custom code using $options['myScales'] and $options[scalesLocale']
    // This builds the relevant $scaleSelect

    // ....

$builder
        ->add('scale', 'choice', array(
            'choices'   => $scaleSelect
        ))
        ->add('subscale', 'collection', array(
            'type' => new subType,
            'prototype' => true,
            'allow_add' => true,
            'allow_delete' => true,
        ))

然后我需要定义嵌套的子类型。它还需要一个自定义的选择框。如何将变量发送给它$scaleSelect以生成(相同)适当的选择框?

4

1 回答 1

0

我想到的最快的选择:

      ->add('subscale', 'collection', array(
           'type' => new Subtype($scaleSelect),
           'prototype' => true,
           'allow_add' => true,
           'allow_delete' => true,
       ))

然后在您的子类型类中:

     function __construct($choices) {
         $this->choices = $choices;
     }

之后,您可以使用 $this->choices 在 buildform 中访问您传递的选项。希望能帮助到你。

于 2013-02-18T20:24:36.683 回答