Skip to content

Commit e7fb22a

Browse files
Ignore empty values. Fixes #39 (#42)
1 parent 1371df5 commit e7fb22a

File tree

4 files changed

+9
-14
lines changed

4 files changed

+9
-14
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ you can set the `strict` option to `FALSE`.
8787

8888
```php
8989
/**
90-
* @ZipCode(getter="getCountry", strict=false)
91-
*/
90+
* @ZipCode(getter="getCountry", strict=false)
91+
*/
9292
protected $zipCode;
9393
```
9494

95-
To avoid that the validation fails in case that there's an empty value in the zip code field
96-
you can set the `ignoreEmpty` option to `TRUE`.
95+
The validator will not validate empty strings and null values. To disallow them use the Symfony stock `NotNull` or `NotBlank` constraint in addition to `ZipCode`.
9796

9897
```php
9998
/**
100-
* @ZipCode(getter="getCountry", ignoreEmpty=true)
101-
*/
99+
* @ZipCode(getter="getCountry")
100+
* @NotBlank
101+
*/
102102
protected $zipCode;
103103
```
104104

src/ZipCodeValidator/Constraints/ZipCode.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ class ZipCode extends Constraint
3434
*/
3535
public $strict = true;
3636

37-
/**
38-
* @var bool
39-
*/
40-
public $ignoreEmpty = false;
41-
4237
/**
4338
* @var bool
4439
*/

src/ZipCodeValidator/Constraints/ZipCodeValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function validate($value, Constraint $constraint)
224224
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\ZipCode');
225225
}
226226

227-
if ($constraint->ignoreEmpty and empty($value)) {
227+
if (null === $value || '' === $value) {
228228
return;
229229
}
230230

tests/Constraints/ZipCodeValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ public function testUnexpectedTypeException()
8888
/**
8989
* @doesNotPerformAssertions
9090
*/
91-
public function testValidationDoesNothingOnEmptyValueIfIgnoreEmptyIsTrue()
91+
public function testValidationIgnoresBlankValues()
9292
{
9393
$constraint = new ZipCode('HK');
94-
$constraint->ignoreEmpty = true;
9594

9695
$this->validator->validate('', $constraint);
96+
$this->validator->validate(null, $constraint);
9797
}
9898

9999
/** @test */

0 commit comments

Comments
 (0)