Symfony2 - 从 formBuilder 内部修改自定义字段类型中的选择

标签 symfony

我通过扩展选择字段 (following the tutorial in the cookbook) 创建了自定义“状态”表单类型。选择字段的选项是根据通过服务注入(inject)的参数自动填充的。但是,我想将此状态字段配置为使用一组简单或扩展的选项,可以是简单的状态列表(不活动、事件)或更完整的状态列表(不活动、事件、删除、已删除)。

我下面的代码根据我的 $options['customOptions'] 成功修改了 $options['choices'],但这些更改对呈现的最终选择字段选项没有影响。

如何从 formBuilder 中修改字段的 choice 选项?

参数.yml

parameters:
    gutensite_component.options.status:
        0: Inactive
        1: Live
    gutensite_component.options.status_preview:
        2: "Preview Only"
        3: "Live Only (delete on sync)"
    gutensite_component.options.status_delete:
        -1: "Delete (soft)"
        -2: Deleted

服务.yml

services:
    gutensite_component.form.type.status:
        class: Gutensite\ComponentBundle\Form\Type\StatusType
        arguments:
            - "%gutensite_component.options.status%"
            - "%gutensite_component.options.status_preview%"
            - "%gutensite_component.options.status_delete%"
        tags:
            - { name: form.type, alias: "status" }

表单类型

class StatusType extends AbstractType
{

    /**
     * $statusChoices are passed in from services.yml that initiates this as a global service
     * @var array
     */
    private $statusChoices;
    private $statusChoicesPreview;
    private $statusChoicesDelete;

    public function __construct(array $statusChoices, array $statusChoicesPreview,
        array $statusChoicesDelete) {
        $this->statusChoices = $statusChoices;
        $this->statusChoicesPreview = $statusChoicesPreview;
        $this->statusChoicesDelete = $statusChoicesDelete;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'customOptions' => array(),
            // By default it will use the simple choices
            'choices' => $this->statusChoices
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Set extra choices based on customOptions
        if(!empty($options['customOptions']['showPreview'])) {
            $options['choices'] = $options['choices'] + $this->staftusChoicesPreview;
        }
        if(!empty($options['customOptions']['showDelete'])) {
            $options['choices'] = $options['choices'] + $this->statusChoicesDelete;
        }

        // The $options['choices'] ARE successfully modified and passed to the parent, but
        // have no effect on the options used in the form display

        // Include Regular Choice buildForm
        parent::buildForm($builder, $options);
    }




    public function getParent() {
        return 'choice';
    }

    public function getName() {
        return 'status';
    }

}

Controller

$builder->add('status', 'status', array(
    'label'     => 'Status',
    'required'  => TRUE,
    'customOptions' => array(
        'showPreview' => true
    )
));

最佳答案

您可以将一组选择从 Controller 传递给表单类:

在 Controller 中:

$myArray = array('0' => 'foo', '1' => 'bar', ...);
...
$form = $this->createForm(new SomeFormType($myArray), ...);

在表单类中:

class SomeFormType extends AbstractType
{

private $choiceArray;

public __construct($array)
{
    $this->choiceArray = $array;
}

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder
            ->add('status', 'choice', array(
                 'choices' => $this->choiceArray,
                  ...
             )
        ...
    }
...
}

关于Symfony2 - 从 formBuilder 内部修改自定义字段类型中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450520/

相关文章:

php - symfony2 Doctrine - 将 child 添加到 self 相关的实体

css - EasyAdminBundle : Add a custom view that's not based on entity

symfony - 如何从 Controller 动态设置表单的级联验证

Symfony2测试参数

php - Symfony 路由 : match anything after first node

php - 无法使用 Symfony Guard 登录,可能是因为 cookie 和多域

html - Symfony 2 - 创建一个带有父实体过滤子实体选项的表单

symfony - Twig/Symfony2 - 在数组合并中使用变量

http - symfony2 重定向响应

REST API 和客户端在同一台服务器上,需要 API 身份验证吗?