Skip to content

Commit

Permalink
Per the [documentation][1], the NotBlank constraint should be using…
Browse files Browse the repository at this point in the history
… the `empty` language construct, otherwise it will not trigger on, for example, a boolean false from an unchecked checkbox field.

[1]: http://symfony.com/doc/2.0/reference/constraints/NotBlank.html
  • Loading branch information
EvanK authored and fabpot committed Sep 2, 2011
1 parent d1ad47c commit 639513a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NotBlankValidator extends ConstraintValidator
*/
public function isValid($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
if (false === $value || (empty($value) && '0' != $value)) {
$this->setMessage($constraint->message);

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ protected function tearDown()
}

/**
* @dataProvider getInvalidValues
* @dataProvider getValidValues
*/
public function testInvalidValues($date)
public function testValidValues($date)
{
$this->assertTrue($this->validator->isValid($date, new NotBlank()));
}

public function getInvalidValues()
public function getValidValues()
{
return array(
array('foobar'),
array(0),
array(false),
array(0.0),
array('0'),
array(1234),
);
}
Expand All @@ -56,6 +57,16 @@ public function testBlankIsInvalid()
$this->assertFalse($this->validator->isValid('', new NotBlank()));
}

public function testFalseIsInvalid()
{
$this->assertFalse($this->validator->isValid(false, new NotBlank()));
}

public function testEmptyArrayIsInvalid()
{
$this->assertFalse($this->validator->isValid(array(), new NotBlank()));
}

public function testSetMessage()
{
$constraint = new NotBlank(array(
Expand All @@ -67,3 +78,4 @@ public function testSetMessage()
$this->assertEquals($this->validator->getMessageParameters(), array());
}
}

0 comments on commit 639513a

Please sign in to comment.