Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store


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 for Zend_Validate_NotEmpty

The following options are supported for Zend_Validate_NotEmpty:

  • type: Sets the type of validation which will be processed. For details take a look into this section.

Default behaviour for Zend_Validate_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.

= new Zend_Validate_NotEmpty();
$value  '';
$result $valid->isValid($value);
// returns false

Default behaviour differs from PHP

Without providing configuration, Zend_Validate_NotEmpty's behaviour differs from PHP.

Changing behaviour for Zend_Validate_NotEmpty

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 accomodate differing needs, Zend_Validate_NotEmpty allows you to configure which types should be validated as empty and which not.

The following types can be handled:

  • boolean: Returns FALSE when the boolean value is FALSE.

  • integer: Returns FALSE when an integer 0 value is given. Per default this validation is not activated and returns TRUE on any integer values.

  • float: Returns FALSE when an float 0.0 value is given. Per default this validation is not activated and returns TRUE on any float values.

  • string: Returns FALSE when an empty string '' is given.

  • zero: Returns FALSE when the single character zero ('0') is given.

  • empty_array: Returns FALSE when an empty array is given.

  • null: Returns FALSE when an NULL value is given.

  • php: Returns FALSE on the same reasons where PHP method empty() would return TRUE.

  • space: Returns FALSE when an string is given which contains only whitespaces.

  • object: Returns TRUE. FALSE will be returned when object is not allowed but an object is given.

  • object_string: Returns FALSE when an object is given and it's __toString() method returns an empty string.

  • object_count: Returns FALSE when an object is given, it has an Countable interface and it's count is 0.

  • all: Returns FALSE on 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 give an array, you can use constants, or you can give a textual string. See the following examples:

// Returns false on 0
$validator = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::INTEGER);

// Returns false on 0 or '0'
$validator = new Zend_Validate_NotEmpty(
Zend_Validate_NotEmpty::INTEGER Zend_NotEmpty::ZERO

// Returns false on 0 or '0'
$validator = new Zend_Validate_NotEmpty(array(

// Returns false on 0 or '0'
$validator = new Zend_Validate_NotEmpty(array(

You can also provide an instance of Zend_Config to set the desired types. To set types after instantiation, use the setType() method.

Zend Framework