symfony: NotBlank should not fail when false is given

The following code resulted in an error:

class Registration
{
    /**
     * @Assert\Type("bool")
     * @Assert\NotBlank
     */
    public $acceptedTerms;
}

$foo = new Registration();
$foo->acceptedTerms = false;
$validator->validate($foo);

For some reason, NotBlank is developed to see false as blank. I can’t see the reason behind this and it would fail in cases like this.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (14 by maintainers)

Most upvoted comments

@jakzal while NotNull will solve the problem, I don’t think I’m using the wrong constraint here. Seeing false as blank is just wrong imo. It’s not too much about this being an unsolveable problem, it’s just that this is against what people expect and if there is no reason to do this, I suggest to remove it in order to avoid confusion.

I’ve faced the same problem. The solution in my case was to use a combination of @Assert\NotNull and @Assert\Type(type="bool"). That will make false work and will validate that value is a boolean value and is not null.

Why not using @Assert\True if you want to force a boolean to be true ?

I agree with this. Surely false is a non-blank value, but it’s likely to be a BC break if it was changed?

Just wasted hours wondering why false wasn’t considered “NotBlank”! 😃

Can this get deprecated already or NotBlank refactored to the new constraint NotEmpty seeing as empty(false) evaluates to true anyway?