?
Referenced as Pull Request for: # 9336
avatar mrDand0r
mrDand0r
18 Jan 2016

Steps to reproduce the issue

  1. Create a number form field in a view form xml with the following minimum settings:
    name="mynumber" 
    type="number" 
    default="0" 
    min="-123" 
    
  2. Create a new item using the above form data
  3. The form field shows a value of -123 (wrong)
  4. Then set the value of the 'mynumber' field to 0 (zero) and save
  5. The form field will again show the min-value of -123 (wrong)

Expected result

Correct behavior when using negative values

Actual result

If the value is set to <= 0 the field value is set to its min-value.
Also the default value is ignored and the field's min-value is used instead.

System information (as much as possible)

Joomla 3.5, PHP 5.6

Additional comments

I did some research on the form field code and it seems that the "empty value detection" is not working as expected. Since empty(0) will return true, the specified min-value will always be used. This is okay as long as you use positive numbers, but fails with negative.

Here's a suggestion on fixing this (File: libraries/joomla/form/fields/number.php:160)

replace

$value = (float) $this->value;
$value = empty($value) ? $this->min : $value;

with

// Make sure to set a valid default value (use min if 'default' not set or 0 if 'default' and 'min' not set)
if($this->value === "") {
     if(isset($this->min)) {
          $value = (float) $this->min;
     } else {
          $value = 0;
     }
} else {
     $value = (float) $this->value;
}
avatar mrDand0r mrDand0r - open - 18 Jan 2016
avatar wilsonge
wilsonge - comment - 18 Jan 2016

You're minimum data seems to have got lost in the first point - could you edit it please :)

avatar mrDand0r mrDand0r - change - 19 Jan 2016
The description was changed
avatar mrDand0r
mrDand0r - comment - 19 Jan 2016

Sorry, just edited my post


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8928.

avatar joomdonation
joomdonation - comment - 19 Jan 2016

I think replace these two lines of code https://github.com/joomla/joomla-cms/blob/staging/libraries/joomla/form/fields/number.php#L160-L161 with this line should solve the issue:

$value = is_numeric($this->value) ? (float) $this->value : $this->min;
avatar mrDand0r
mrDand0r - comment - 19 Jan 2016

Yes, this fixes the issue.

But if optional "default" and "min" options are not set, $value will get null on an empty form.
Maybe it should be set to 0 to perfect this solution.

Something like this?


$this->min = isset($this->min) ? $this->min : 0;
$value = is_numeric($this->value) ? (float) $this->value : $this->min;

This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/8928.

avatar mrDand0r mrDand0r - change - 19 Jan 2016
The description was changed
avatar brianteeman
brianteeman - comment - 7 Mar 2016

Closing as we have a PR for testing - see #9336

avatar brianteeman brianteeman - change - 7 Mar 2016
Status New Closed
Closed_Date 0000-00-00 00:00:00 2016-03-07 22:48:45
Closed_By brianteeman
avatar brianteeman brianteeman - close - 7 Mar 2016

Add a Comment

Login with GitHub to post a comment