将表单分解为更小的FormTypes

时间:2017-05-31 14:22:19

标签: symfony symfony-forms

我有一个例如Person的表格。一个Person实体有各种属性,如名字,姓氏,眼睛颜色,发色等等。一切都很好。我创建了一个PersonFormType,并通过调用add()来构造表单: -

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstName', TextFilterType::class, [
                'condition_pattern'=>FilterOperands::OPERAND_SELECTOR,
                'required' => false,
                'label' => "person.firstName",
                'error_bubbling' => false,
            ]
        )
        ->add('surname', TextFilterType::class, [
            'condition_pattern'=>FilterOperands::OPERAND_SELECTOR,
            'required' => false,
            "label" => "person.surname",
        ])

现在假设我想将名字和姓氏分解为NameFilterType: -

class NameFilterType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', TextFilterType::class, [
                    'condition_pattern'=>FilterOperands::OPERAND_SELECTOR,
                    'label' => "person.firstName",
                    'error_bubbling' => false,
                ]
            )
            ->add('surname', TextFilterType::class, [
                'condition_pattern'=>FilterOperands::OPERAND_SELECTOR,
                'required' => false,
                "label" => "person.surname",
            ]);
    }

    public function getBlockPrefix()
    {
        return 'name_filter';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'required' => false,
            'data_class' => "AppBundle\Entity\Person",
            'translation_domain' => null,
        ]);
    }

}

现在问题是我如何将其添加到原始表单中?我猜我错过了一些明显的东西。但是,如果我尝试类似的东西:

$builder
            ->add('name', NameFilterType::class, []);

在运行时它不起作用,因为get参数是

  

person_filter.name.firstname

例如,

。如何告诉symfony的表单机制忽略此嵌入式表单片段的“名称”的“逻辑名称”?

0 个答案:

没有答案