简体   繁体   English

Symfony2 FormType可选整体或全无

[英]Symfony2 FormType optional whole or nothing

I have form in Symfony2 containing 2 sub form types representing 2 relations in my entity. 我在Symfony2中有一个表单,其中包含2个子表单类型,它们代表我实体中的2个关系。 invoiceAddress is required always and it works OK, but i want deliveryAddress keep optional only when all inputs of deliveryAdress are empty. invoiceAddress始终是必需的,并且可以正常运行,但是我希望deliveryAddress仅在deliveryAdress的所有输入均为空时保持可选。 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. 当我设置为不需要时,symfony在表单中未填写任何内容时将deliveryAddress设置为null,但是在填充某些字段时,不会在该实体上运行断言的验证。 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. 所以我想要实现的是告诉symfony当表单完全为空时,然后将deliveryAddress设置为null,但是当表单中的某些内容被设置后,以常规方式运行验证。 Is there any way how to do it? 有什么办法吗? Thanks. 谢谢。

My form type looks like this and in controller i have standart $form->isValid condition. 我的表单类型看起来像这样,在控制器中,我有标准的$ form-> isValid条件。

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

AdressType contains only some primitive type mappings like this: AdressType仅包含一些原始类型映射,如下所示:

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

And in Address entity: 并在地址实体中:

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

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

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. Address对象为空时,表单组件将返回null作为模型数据并将其设置在父对象上。 When a property is null the validation is skipped for that property. 当属性为null ,将跳过对该属性的验证。 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM