Symfony3:验证嵌套(可选)表单

时间:2016-10-09 13:19:15

标签: php forms symfony symfony-forms

我的项目中有三个文件:Event,OrganizerProfile和User。

用户可以拥有多个OrganizerProfile(就像Facebook上的#39;页面')和事件。

当用户创建和事件时,他可以分配" OrganizerProfile"参加活动(User Alberto为" A公司和#34创建了一个活动;它被称为"活动X")。

为实现这一目标,我已经创建了这种形式:

OrganizerProfileType.php

class OrganizerProfileType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder
            ->add('email', EmailType::class)
            ->add('name', TextType::class)
            ->add('description', TextType::class, ['required' => false])
...

EventType.php

class EventType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $profileChoices = ... //List of existing profiles

        $builder
            ->add('profile_list', ChoiceType::class, [
                'choices' => $profileChoices,
                'required' => false,
                'mapped' => false,
            ])
            ->add('profile', OrganizerProfileType::class, [
                'required' => true,
            ])
            ->add('title', TextType::class, ['required' => false])
            ->add('description', TextType::class, ['required' => false])
...

在字段" profile_list"用户可以找到他们现有的OrganizerProfiles。用户可以选择其中一个并将其分配给事件但是如果用户没有选择现有的个人资料,他必须在"个人资料中插入信息。形式。

我想制作"可选"个人资料表单仅在用户不选择现有个人资料时才需要。

我该怎么做?感谢

1 个答案:

答案 0 :(得分:0)

您可以按Validation Groups Based on the Submitted Data实施。

所以你的FormType应该如下:

class EventType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $profileChoices = [...];

        $builder
            ->add('profile_list', ChoiceType::class, [
                'choices'  => $profileChoices,
                'required' => false,
                'mapped'   => false,
            ])
            ->add('profile', OrganizerProfileType::class, [
                'constraints' => [
                    new Valid()
                ]
            ])
            ->add('title', TextType::class, ['required' => false])
            ->add('description', TextType::class, ['required' => false]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'validation_groups' => function (FormInterface $form) {

                if (null === $form->get('profile_list')->getData()) {
                    return ['without_profile'];
                }

                return ['with_profile'];  // Or simply 'Default'
            },
        ]);
    }

}

更新:使用另一种FormType中的验证组

然后,您需要使用验证组处理子表单的验证,例如:

class OrganizerProfileType extends AbstractType{

    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder
            ->add('email', EmailType::class, [
                    'constraints' => [
                        new NotNull(
                            [
                                'groups' => ['without_profile', 'with_profile']
                            ]
                        )
            ]])
            ->add('name', TextType::class, [
                    'constraints' => [
                        new NotNull(
                            [
                                'groups' => ['without_profile', 'with_profile']
                            ]
                        )
            ]])
            ->add('description', TextType::class, ['required' => false])

希望这个帮助