History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: ZF-2851
Type: Patch Patch
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Matthew Weier O'Phinney
Reporter: Alexander Lanin
Votes: 2
Watchers: 4
Operations

If you were logged in you would be able to see more operations.
Google issue summary
Zend Framework

value "0" is treated as empty

Created: 10/Mar/08 06:41 PM   Updated: 07/Aug/08 11:41 AM
Component/s: Zend_Form
Affects Version/s: 1.5.0RC1
Fix Version/s: 1.5.2

Time Tracking:
Original Estimate: 15 minutes
Original Estimate - 15 minutes
Remaining Estimate: 15 minutes
Remaining Estimate - 15 minutes
Time Spent: Not Specified
Remaining Estimate - 15 minutes

Issue Links:
Duplicate
 

 Public Fields   Internal Project Management Fields   
Resolution Date: 02/May/08 12:20 PM
Fix Version Priority: Must Have


 Description  « Hide
bug:
$form->addElement("text", "abc");
$form->abc->setLabel("please enter a zero")->setRequired(true);

this will be impossible

bug2:

$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...

fix:

Index: Form/Element.php
===================================================================
--- Form/Element.php	(revision 8741)
+++ Form/Element.php	(working copy)
@@ -1146,7 +1147,7 @@
         $this->setValue($value);
         $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 @@
 
         $this->_setValue($valueString);
 
-        if (empty($value)) {
+        if ($value === "") {
             $this->_error();
             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');
         }


 All   Comments   Work Log   Change History   FishEye   Crucible      Sort Order: Ascending order - Click to sort in descending order
Vladimir Michailenko - 11/Mar/08 12:59 PM
Also option in Zend_Form_Element_Multiselect with key "0" can not be marked as 'selected' via ->populate() or ->setValue(). For example: http://www.nabble.com/Zend_Form_Element_Multiselect-and-option-with-key-'0'-td15853694s16154.html

Matthew Weier O'Phinney - 12/Mar/08 07:46 AM
Scheduling for final release

Alexander Lanin - 14/Mar/08 07:13 AM
I haven't gotten to multiselect yet, sry
maybe in a few days or matthew will fix it

Vladimir Michailenko - 14/Mar/08 07:28 AM
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)


Matthew Weier O'Phinney - 14/Mar/08 07:38 AM
Re-scheduling for 1.5.1.

Wil Sinclair - 25/Mar/08 10:02 PM
Resetting 'fix version priority' and 'fix version' to be re-evaluated for next release.

Gregory Wilson - 07/Apr/08 12:16 PM
Ran into the same issue here. No one on #zftalk knew anything about it either.

Alexander Lanin - 07/Apr/08 01:37 PM
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()

Matthew Weier O'Phinney - 22/Apr/08 11:45 AM
Scheduling for next mini release.

Matthew Weier O'Phinney - 02/May/08 12:20 PM
Resolved in trunk and 1.5 release branch

Vladimir Michailenko - 09/May/08 03:16 AM
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)) { $this->_error(); return false; }

php.net/empty:

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)


Gregory Wilson - 16/May/08 10:54 AM
Confirmed still an issue in 1.5.2 release.

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


Cole Snodgrass - 07/Aug/08 10:40 AM
This is still an issue in 1.6.0rc1, I would reopen this issue but lack the ability.
<?php
require_once 'Zend/Version.php';
require_once 'Zend/Validate.php';

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

/*
Expected output:
string(3) "1.6"
bool(true)
bool(true)

Actual output:
string(3) "1.6"
bool(true)
bool(false)
*/

Matthew Weier O'Phinney - 07/Aug/08 11:41 AM
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.