1
votes

I'm setting up a form through annotations using

/**
 * @ORM\Column(type="boolean", nullable=false, name="is_public")
 * @Annotation\Required(false)
 * @Annotation\AllowEmpty()
 * @Annotation\Attributes({"placeholder":"Is Public"})
 * @Annotation\Filter({"name":"boolean"})
 * @Annotation\Options({"label":"Is Public"})
 */
private $isPublic;

This form is built using the doctrine annotation builder and the doctrine entity hydrator. The entity is then bound to this form. There is an issue when passing a boolean field, in that any value is treated as false, except 1, passing 0 results in an error message of "cannot be empty".

Can somebody please advise as to how I can use boolean fields properly using this method? Ideally I'd like to be able to use the filter before the field is validated? Not only that, but the validation is ignoring the AllowEmpty() and Required(false) fields.

Kind Regards,

ise

2

2 Answers

0
votes

What you say is happening seems correct. Checkbox on forms submit nothing when unchecked, but the hydrator obviously needs to be able to know when the user intends to clear the value (mark false).

Pretty sure hydrator should work with empty string for false too. Required and AllowEmpty don't really make sense with a Boolean, especially in your case, because you also put nullable=false

0
votes

This way I solved it $form->getInputFilter()->get('isPublic')->setContinueIfEmpty(true); just before $form->isValid()