Optional embed form in Symfony 2

前端 未结 5 1967
予麋鹿
予麋鹿 2021-01-16 11:00

I have two entities in my system: Person and Phone as the following code.

class Person
{
    /**
     * @ORM\\Id
     * @ORM\\Colum         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 11:14

    Do this:

    First on your phone entity, declare the fields as "nullable" like this:

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $countryCode;
    

    This will allow these fields to have a null value.

    Then you need to declare a validator that will check if either none of the fields or all of them are filled

    class Phone {
    
    ...
    /**
     * @Assert\True(message = "Phone must be empty of fill all fields")
     */
    public function isPhoneOK()
    {
        // check if no field is null or all of them are
        // return false if these conditions are not met
    }
    

提交回复
热议问题