5
votes

I have a Doctrine entity that has boolean field. Should I add Symfony validation for it (for type boolean), or my form is correctly validated by inferring the type automatically?

class Entity
{
    /**
     * @ORM\Column(type="boolean")
     * @Assert\.... <- do I have to apply any Symfony assertion here?
     */
    private $isActive;
}
2

2 Answers

7
votes

No, you have only two cases. Value is present or not - so true or false. I imagine a validator only in one case if this checkbox has to be set by user always like "accept disclaimer" during registration process

1
votes

In addition to what Lazy Ants has said, you would only need to assert the type as bool if this field is nullable. -- That's because null and false are not identical.

You would only need this scenario if not all of the entity is going to be populated immediately though, for example a multi step form you're going to persist across each step hop or have auto-save capability for. If the entire entity is being populated in a single request the property should not be nullable.