Validators
In This Article
NotEmpty Validator
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.
Supported options
The following options are supported for Zend\Validator\NotEmpty:
type: Sets the type of validation which will be processed; for details, see the section on specifying empty behavior.
Default behaviour
By default, this validator works differently than you would expect when you've
worked with PHP's empty() operator. In particular, this validator will
evaluate both the integer 0 and string '0' as empty.
$valid = new Zend\Validator\NotEmpty();
$value = '';
$result = $valid->isValid($value);
// returns false
Specifying empty behavior
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:
boolean: Returnsfalsewhen the boolean value isfalse.integer: Returnsfalsewhen an integer0value is given. By default, this validation is not activate and returnstruefor any integer values.float: Returnsfalsewhen a float0.0value is given. By default, this validation is not activate and returnstrueon any float values.string: Returnsfalsewhen an empty string''is given.zero: Returnsfalsewhen the single character zero ('0') is given.empty_array: Returnsfalsewhen an emptyarrayis given.null: Returnsfalsewhen anullvalue is given.php: Returnsfalseon wherever PHP'sempty()would returntrue.space: Returnsfalsewhen an string is given which contains only whitespace.object: Returnstrue.falsewill be returned whenobjectis not allowed but an object is given.object_string: Returnsfalsewhen an object is given and its__toString()method returns an empty string.object_count: Returnsfalsewhen an object is given, it implementsCountable, and its count is 0.all: Returnsfalseon all above types.
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 provide an array, you can use constants, or you can provide a textual string. See the following examples:
use Zend\Validator\NotEmpty;
// Returns false on 0
$validator = new NotEmpty(NotEmpty::INTEGER);
// Returns false on 0 or '0'
$validator = new NotEmpty( NotEmpty::INTEGER | NotEmpty::ZERO);
// Returns false on 0 or '0'
$validator = new NotEmpty([ NotEmpty::INTEGER, NotEmpty::ZERO ]);
// Returns false on 0 or '0'
$validator = new NotEmpty(['integer', 'zero']);
You can also provide an instance of Traversable to set the desired types. To
set types after instantiation, use the setType() method.
Found a mistake or want to contribute to the documentation? Edit this page on GitHub!