首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有Symfony的可空自定义表单实体

带有Symfony的可空自定义表单实体
EN

Stack Overflow用户
提问于 2016-05-02 05:42:41
回答 1查看 3.6K关注 0票数 21

我的应用程序管理族。一个族由1个或N个成员组成。

我希望有可能添加一个或两个父级和0或N个子级。孩子部分工作得很好,但我很难与一两个父母打交道。

以下是我的家庭表单类型:

 $builder
        ... many attributes
        ->add('parent1', MemberType::class)
        ->add('parent2', MemberType::class)

Parent和parent2是OneToOne关联(家族到成员)。成员表单类型:

 $builder
        ->add('firstName', TextType::class, [
            'label' => 'Prénom',
            'constraints' => array(
                new NotBlank(),
                new Length(array('max' => 150))
            )
        ])
        ... many other attributes with choices or not

我想到了一个复选框,如果没有选中,它会灰显parent 2的字段,但成员值都是必需的。因此,SF2不会验证我的表单。

如果我为这些字段设置了required => false (在构建器中),那么用户将有可能在不填写所有内容的情况下进行验证(我不希望这样)。

我想创建以下流程:

  • 我们要么填写member2的所有字段以验证表单
  • 要么选中复选框(单父)且不需要任何字段,我的最终member2将为null (或另一个解决方案)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-06 19:05:26

在阅读了大量文档之后,我在这里找到了问题的解决方案:http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

为了使实体不是必需的,您应该添加事件侦听器并将数据设置为null post submit。

第一步

orphanRemoval=true选项添加到属性

/**
 * @ORM\OneToOne(targetEntity="AppBundle\Entity\Member", orphanRemoval=true, cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="parent2_id", referencedColumnName="id",nullable=true)
 */
private $parent2;

第二步

在表单中添加一个新字段,即未映射复选框

   $builder
        ->add('parent1', MemberType::class)
        ->add('withParent2', CheckboxType::class, [
            'mapped'            => false,
            'required'          => false,
            'data'              => true
        ])
        ->add('parent2', MemberType::class, [
            'required'          => false
        ])

如果未选中,我们将使用此复选框将parent2设置为null。

在此旁边,添加事件侦听器:

   //this event will set whether or not the checkbox should be checked
   $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
        $form = $event->getForm();
        $family = $event->getData();

        if ($family->getId()) {
            $form->add('withParent2', CheckboxType::class, [
                'mapped'        => false,
                'required'      => false,
                'data'          => $family->getParent2() ? true : false
            ]);
        }
    });

    //Event when the form is submitted, before database update
    $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {

        //if the checkbox was not checked, it means that there was not a second parent
        $withParent2 = $event->getForm()->get('withParent2')->getData();
        if (!$withParent2) {

            // we set this attribute to null, and disable the form validation
            $event->getData()->setParent2(null);
            $event->stopPropagation();
        }

    }, 900);

第三步

我们的表单以这种方式工作得很好,剩下的唯一问题就是javascript验证。

只需执行一个jquery函数,从字段中删除必需的属性。

 function toggleParent2Requirement(checked){
        if (!checked) {
            $("[id^=family_parent2]").prop("required", false);
            $("[id^=family_parent2]").attr('disabled', true);
        }
        else {
            $("[id^=family_parent2]").prop("required", true);
            $("[id^=family_parent2]").attr('disabled', false);
        }
    }

在这里,您可以使oneToOne关系成为可选的。我唯一不感到自豪的部分是stopPropagation部分。这是在文档中,我不知道我们是否可以禁用这个字段的验证以一种更干净的方式。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36972308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档