Zend Framework

_getParam in Controller/Action don't handle 0

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.7.1
  • Fix Version/s: 1.9.7
  • Component/s: Zend_Controller
  • Labels:
    None

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;
}

  1. ZF-5163.patch
    15/Dec/09 5:12 AM
    0.4 kB
    Menno Luiten
  2. ZF-5163.test.patch
    17/Dec/09 2:41 PM
    1 kB
    Menno Luiten

Issue Links

Activity

Hide
Satoru Yoshida added a comment -

Set component and auto reassign

Show
Satoru Yoshida added a comment - Set component and auto reassign
Hide
Jonathan Melnick added a comment -

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!

Show
Jonathan Melnick added a comment - 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!
Hide
Harro van der Klauw added a comment -

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.

Show
Harro van der Klauw added a comment - 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.
Hide
Johan Isacsson added a comment -

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.

Show
Johan Isacsson added a comment - 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.
Hide
Gilles Bouthenot added a comment -

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.

Show
Gilles Bouthenot added a comment - 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.
Hide
Menno Luiten added a comment - - edited

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

Show
Menno Luiten added a comment - - edited 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
Hide
Menno Luiten added a comment -

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?

Show
Menno Luiten added a comment - 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?
Hide
Menno Luiten added a comment -

Resolved in r19763

Show
Menno Luiten added a comment - Resolved in r19763

People

Vote (4)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: