Symfony2 FormType 可选整体或无 [英] Symfony2 FormType optional whole or nothing

查看:28
本文介绍了Symfony2 FormType 可选整体或无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Symfony2 中有表单,其中包含 2 个子表单类型,代表我的实体中的 2 个关系.invoiceAddress 始终是必需的,它工作正常,但我希望 deliveryAddress 仅在 deliveryAdress 的所有输入都为空时保持可选.当我将它设置为不需要时,symfony 将 deliveryAddress 设置为 null 当表单中没有填写任何内容时,但是当填充某些字段时,它不会对该实体运行断言验证.所以我想要实现的是告诉 symfony 当表单为空时,然后将 deliveryAddress 设置为 null,但是当表单中的某些内容被设置时,然后以正常方式运行验证.有什么办法可以做到吗?谢谢.

I have form in Symfony2 containing 2 sub form types representing 2 relations in my entity. invoiceAddress is required always and it works OK, but i want deliveryAddress keep optional only when all inputs of deliveryAdress are empty. When i set it like not required symfony sets deliveryAddress as null when nothing is filled in form, but when some fields are filled it do not run validation of asserts on that entity. So what i am trying to achieve is tell the symfony when form is whole empty then set deliveryAddress as null but when something in form is set then run validations in normal way. Is there any way how to do it? Thanks.

我的表单类型看起来像这样,在控制器中我有标准的 $form->isValid 条件.

My form type looks like this and in controller i have standart $form->isValid condition.

$builder->add('invoiceAddress',new AddressType())
        ->add('deliveryAddress', new AddressType(),["required" => false])

AdressType 只包含一些像这样的原始类型映射:

AdressType contains only some primitive type mappings like this:

$builder->add('firstName')
        ->add('lastName')...

在地址实体中:

    /**
 * @Assert\NotBlank
 * @ORM\Column
 * @var string
 */
private $firstName;

/**
 * @Assert\NotBlank
 * @ORM\Column
 * @var string
 */
private $lastName;

推荐答案

如果您正确设置了实体和断言,这应该是开箱即用的.当 Address 对象为空时,表单组件将返回 null 作为模型数据并将其设置在父级上.当属性为 null 时,将跳过对该属性的验证.它应该看起来像这样:

If you setup entities and assertions correctly this should work out of the box. When the Address object is empty the form component will return null as model data and set it on the parent. When a property is null the validation is skipped for that property. It should look something like this:

class Order
{
    /**
     * @var Address
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Address")
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
     *
     * @Assert\NotBlank()
     * @Assert\Valid()
     */
    private $invoiceAddress;

    /**
     * @var Address
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Address")
     * @ORM\JoinColumn(onDelete="SET NULL", nullable=true)
     *
     * @Assert\Valid()
     */
    private $deliveryAddress;

    public function getInvoiceAddress()
    {
        return $this->invoiceAddress;
    }

    public function setInvoiceAddress(Address $address)
    {
        $this->invoiceAddress = $address;
    }

    public function getDeliveryAddress()
    {
        return $this->deliveryAddress;
    }

    public function setDeliveryAddress(Address $address = null)
    {
        $this->deliveryAddress = $address;
    }
}

这篇关于Symfony2 FormType 可选整体或无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆