ZF-2851: value "0" is treated as empty



$form->addElement("text", "abc");
$form->abc->setLabel("please enter a zero")->setRequired(true);

this will be impossible


$form->addElement("text", "abc");
$form->abc->setLabel("enter a number (1 to 100)")->addValidator("between", false, array(1, 100));

user enters a 0 and it's ok!

note: I typed the above bugs right here, so there might be some things spelled wrong etc...


Index: Form/Element.php
--- Form/Element.php    (revision 8741)
+++ Form/Element.php    (working copy)
@@ -1146,7 +1147,7 @@
         $value = $this->getValue();
-        if (empty($value) && !$this->isRequired() && $this->getAllowEmpty()) {
+        if ($value === "" && !$this->isRequired() && $this->getAllowEmpty()) {
             return true;
Index: Validate/NotEmpty.php
--- Validate/NotEmpty.php   (revision 8741)
+++ Validate/NotEmpty.php   (working copy)
@@ -59,7 +59,7 @@
-        if (empty($value)) {
+        if ($value === "") {
             return false;

// edit, new:

Index: Form/Element.php
--- Form/Element.php    (revision 8819)
+++ Form/Element.php    (working copy)
@@ -407,7 +407,7 @@
     public function setName($name)
         $name = $this->filtername($name);
-        if (('0' !== $name) && empty($name)) {
+        if ($name === '') {
             require_once 'Zend/Form/Exception.php';
             throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');


Also option in Zend_Form_Element_Multiselect with key "0" can not be marked as 'selected' via ->populate() or ->setValue(). For example: http://nabble.com/Zend_Form_Element_Multiselect-an…

Scheduling for final release

I haven't gotten to multiselect yet, sry maybe in a few days or matthew will fix it ;-)

Your fix NotEmpty validator does not handle empty array: $value = array();

May be better just add 2 exceptions for ( http://php.net/empty ): 0 (0 as an integer) "0" (0 as a string)

Re-scheduling for 1.5.1.

Resetting 'fix version priority' and 'fix version' to be re-evaluated for next release.

Ran into the same issue here. No one on #zftalk knew anything about it either.

I'm verry sorry, but someone else will have to fix this as I don't have the time right now. One note: empty() doesn't make sense, since all we need is to check for "" and array(), this are 2 "operations" instead of 7 with empty()

Scheduling for next mini release.

Resolved in trunk and 1.5 release branch

Version from trunk:

    $this->addElement('select', 'parent_id', array( 
        'label' => 'Category',
        'multiOptions' => array(
            '0' => 'Root',
                 'attribs' => array(
            'size' => 4,
                 'required' => true,

When I choose "Root" category I still get error... Zend_Validate_NotEmpty:

    if (empty($value)) {
        return false;


The following things are considered to be empty: "" (an empty string) 0 (0 as an integer) !!! "0" (0 as a string) !!! NULL FALSE array() (an empty array) var $var; (a variable declared, but without a value in a class)

Confirmed still an issue in 1.5.2 release.

Zend_Debug::dump(Zend_Validate::is('0', 'NotEmpty')); // false

This is still an issue in 1.6.0rc1, I would reopen this issue but lack the ability.

require_once 'Zend/Version.php';
require_once 'Zend/Validate.php';

var_dump(Zend_Validate::is('1', 'NotEmpty'));
var_dump(Zend_Validate::is('0', 'NotEmpty'));

Expected output:
string(3) "1.6"

Actual output:
string(3) "1.6"

The modification was in Zend_Form. Zend_Validate still retains the old behavior, as this is correct in terms of how empty() works. Please open a separate issue if you feel Zend_Validate_NotEmpty should be changed; be prepared to make a very good case for changing it.