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

Issue Type: Patch Created: 2008-03-10T18:41:01.000+0000 Last Updated: 2008-08-07T11:41:14.000+0000 Status: Resolved Fix version(s): - 1.5.2 (15/May/08)

Reporter: Alexander Lanin (alexl) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: - ZF-3236




<pre class="highlight">$form->addElement("text", "abc");
$form->abc->setLabel("please enter a zero")->setRequired(true);

this will be impossible


<pre class="highlight">$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...


<pre class="highlight">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:

<pre class="highlight">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');


Posted by Vladimir Michailenko (mich) on 2008-03-11T12:59:57.000+0000

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

Posted by Matthew Weier O'Phinney (matthew) on 2008-03-12T07:46:02.000+0000

Scheduling for final release

Posted by Alexander Lanin (alexl) on 2008-03-14T07:13:39.000+0000

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

Posted by Vladimir Michailenko (mich) on 2008-03-14T07:28:16.000+0000

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

May be better just add 2 exceptions for ( ): 0 (0 as an integer) "0" (0 as a string)

Posted by Matthew Weier O'Phinney (matthew) on 2008-03-14T07:38:14.000+0000

Re-scheduling for 1.5.1.

Posted by Wil Sinclair (wil) on 2008-03-25T22:02:49.000+0000

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

Posted by Gregory Wilson (drakos7) on 2008-04-07T12:16:39.000+0000

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

Posted by Alexander Lanin (alexl) on 2008-04-07T13:37:40.000+0000

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()

Posted by Matthew Weier O'Phinney (matthew) on 2008-04-22T11:45:58.000+0000

Scheduling for next mini release.

Posted by Matthew Weier O'Phinney (matthew) on 2008-05-02T12:20:52.000+0000

Resolved in trunk and 1.5 release branch

Posted by Vladimir Michailenko (mich) on 2008-05-09T03:16:53.000+0000

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)

Posted by Gregory Wilson (drakos7) on 2008-05-16T10:54:14.000+0000

Confirmed still an issue in 1.5.2 release.

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

Posted by Cole Snodgrass (cole.snodgrass) on 2008-08-07T10:40:17.000+0000

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

<pre class="highlight">
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"

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-07T11:41:14.000+0000

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.

Have you found an issue?

See the Overview section for more details.


© 2006-2020 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.