ZF-5163: _getParam in Controller/Action don't handle 0
Description
Example $this->_setParam('foo', 0); $foo = $this->_getParam('foo', -1);
$foo is set to -1 instead of the expected value 0
This is because the method in Controller/Action.php does a null == $value check instead of null === $value. It should be modified to look like this: protected function _getParam($paramName, $default = null) { $value = $this->getRequest()->getParam($paramName); if ((null === $value) && (null !== $default)) { $value = $default; }
return $value;
}
Comments
Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2008-12-13T17:55:32.000+0000
Set component and auto reassign
Posted by Jonathan Melnick (jonathan.melnick) on 2009-04-07T03:38:10.000+0000
This issue still affects version 1.7.8 !!
The bug also occurs if param's value is false.
By any means, this is not minor!
Posted by Harro van der Klauw (oximoron) on 2009-04-08T05:17:30.000+0000
The params come from the url, so technically they will always be strings, the number 0 and a boolean value aren't really possible to be part of the params.
but I agree that for sanity's sake it should be fixed.
Posted by Johan Isacsson (cjne) on 2009-04-08T05:28:22.000+0000
In my case i was setting values in one action using _setParam and then used _getParam in another action, so it does not always have to come from the URL.
Posted by Gilles Bouthenot (megar) on 2009-11-19T06:24:18.000+0000
This is a MAJOR issue !
As proposed the simple patch if ((null == $value) && (null !== $default)) {
->
if ((null === $value) && (null !== $default)) {
Fixes it. Problem with 0 and "", both handled like param not present.
Please fix it ASAP.
Posted by Menno Luiten (mluiten) on 2009-12-15T05:12:24.000+0000
Attached (simple) patch implementing the suggested solution, if this is deemed a bug by upstream. I would suggest it is.
~My CLA has been submitted, -but not yet processed-~
Posted by Menno Luiten (mluiten) on 2009-12-17T14:41:24.000+0000
Attached a unit test that tests the conditions of this bug. Is it a good idea to add these conditions (0 as value for setParam) to other unit tests like getParams as well?
Posted by Menno Luiten (mluiten) on 2009-12-18T13:21:29.000+0000
Resolved in r19763