Symfony表单类型:将用户域传递给configureOptions

时间:2018-02-12 11:35:26

标签: php symfony dependency-injection symfony-forms

如何将当前用户域传递给Symfony表单类型?

我的服务如下:

# src/AppBundle/Service/GenericFormService.php

    $domain = 'mydomain';

    $formBuilder = $this->formFactory->createBuilder(
        GenericOrderType::class,
        $order,
        array_merge(
            array(
                'generic_form' => $userForm,
                'validator' => $this->validator,
                'constraints' => $constraints
            ),
            $options
        )
    );

我的表单类型为:

# src/AppBundle/Form/GenericOrderType.php

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => Order::class,
                'translation_domain' => 'NEEDS_THIS_DYNAMIC',
            ]
        );
    }

1 个答案:

答案 0 :(得分:0)

我在发布问题后找到了答案:

# src/AppBundle/Service/GenericFormService.php

    $domain = 'mydomain';
    $options['translation_domain'] = $domain;

    $formBuilder = $this->formFactory->createBuilder(
        GenericOrderType::class,
        $order,
        array_merge(
            array(
                'generic_form' => $userForm,
                'validator' => $this->validator,
                'constraints' => $constraints
            ),
            $options
        )
    );