Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at:
NotEmpty Validator — Zend Framework 2 2.4.9 documentation
This validator allows you to validate if a given value is not empty. This is often useful when working with form elements or other user input, where you can use it to ensure required elements have values associated with them.
The following options are supported for Zend\Validator\NotEmpty:
By default, this validator works differently than you would expect when you’ve worked with PHP‘s empty() function. In particular, this validator will evaluate both the integer 0 and string ‘0‘ as empty.
1 2 3 4
$valid = new Zend\Validator\NotEmpty(); $value = ''; $result = $valid->isValid($value); // returns false
Default behaviour differs from PHP
Without providing configuration, Zend\Validator\NotEmpty‘s behaviour differs from PHP.
Some projects have differing opinions of what is considered an “empty” value: a string with only whitespace might be considered empty, or 0 may be considered non-empty (particularly for boolean sequences). To accommodate differing needs, Zend\Validator\NotEmpty allows you to configure which types should be validated as empty and which not.
The following types can be handled:
All other given values will return TRUE per default.
There are several ways to select which of the above types are validated. You can give one or multiple types and add them, you can give an array, you can use constants, or you can give a textual string. See the following examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Returns false on 0 $validator = new Zend\Validator\NotEmpty(Zend\Validator\NotEmpty::INTEGER); // Returns false on 0 or '0' $validator = new Zend\Validator\NotEmpty( Zend\Validator\NotEmpty::INTEGER + Zend\Validator\NotEmpty::ZERO ); // Returns false on 0 or '0' $validator = new Zend\Validator\NotEmpty(array( Zend\Validator\NotEmpty::INTEGER, Zend\Validator\NotEmpty::ZERO )); // Returns false on 0 or '0' $validator = new Zend\Validator\NotEmpty(array( 'integer', 'zero', ));
You can also provide an instance of Traversable to set the desired types. To set types after instantiation, use the setType() method.