? ? Pending

User tests: Successful: Unsuccessful:

avatar astridx
astridx
23 Jan 2020

Pull Request for Issue in the German forum.

Summary of Changes

Cast a default Custom Field value of 0 to -0.

Testing Instructions

  1. Create a Custom Field of type radio and set the default value to 0 but add no value with 0.
    Beiträge  Feld bearbeiten   test   Administration
  2. Save the Custom Field.

Expected result

You see an error if you want to save a custom field with a default value, that is not possible.

Actual result

Saving is possible.

Documentation Changes Required

No

Note

I cast this value because the function empty is used twice: Here and here in the function test.

avatar astridx astridx - open - 23 Jan 2020
avatar astridx astridx - change - 23 Jan 2020
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 23 Jan 2020
Category Administration com_fields
avatar N6REJ N6REJ - test_item - 27 Jan 2020 - Tested successfully
avatar N6REJ
N6REJ - comment - 27 Jan 2020

I have tested this item successfully on ecb7260


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

avatar jwaisner jwaisner - test_item - 28 Jan 2020 - Tested successfully
avatar jwaisner
jwaisner - comment - 28 Jan 2020

I have tested this item successfully on ecb7260


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

avatar Quy
Quy - comment - 30 Jan 2020

Not able to reproduce with v3.9.15.

27618

avatar astridx
astridx - comment - 31 Jan 2020

Strange, I am able to reproduce with v3.9.15 today.

avatar Quy
Quy - comment - 4 Feb 2020

Sorry false alarm. I had applied your PR when testing. Duh! To me, it feels hackish fix.

avatar astridx astridx - change - 4 Feb 2020
Labels Added: ?
avatar astridx
astridx - comment - 4 Feb 2020

You are right, this is hackish!
It would be more correct to change empty() to isset().

The problem is that emty("0") is interpreted as "there is no default value". Even "0" as a string! That is not correct. There is a default value in our case!

Do you like my new version better?

avatar Quy
Quy - comment - 5 Feb 2020

Now it is always invalid.

avatar joomla-cms-bot joomla-cms-bot - change - 8 Feb 2020
Category Administration com_fields Administration com_fields Libraries
avatar astridx
astridx - comment - 8 Feb 2020

@Quy The last change is embarrassing to me.

Now it should fit.

The following things are considered to be empty (https://www.php.net/empty):

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
    and
  • var $var; (a variable declared, but without a value in a class)

If I am correct, we have to treat the case string "0" differently.

avatar astridx
astridx - comment - 8 Feb 2020

@N6REJ I would be happy if you could test again

02305c3 9 Feb 2020 avatar astridx wip
8a61ac9 9 Feb 2020 avatar astridx wip
avatar astridx
astridx - comment - 12 Feb 2020

Requested updates are done.

avatar Quy Quy - test_item - 13 Feb 2020 - Tested successfully
avatar Quy
Quy - comment - 13 Feb 2020

I have tested this item successfully on 8a61ac9


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

avatar SharkyKZ
SharkyKZ - comment - 13 Feb 2020

Having a closer look, integer 0 and float 0.0 should also pass. So it would probably be better to compare against actually empty values (null, empty string and maybe empty array) rather than do empty() check. And we might need this for other validation rules as well.

avatar astridx
astridx - comment - 14 Feb 2020

@SharkyKZ Having a closer look, integer 0 and float 0.0 should also pass. So it would probably be better to compare against actually empty values (null, empty string and maybe empty array) rather than do empty() check.

I hope I understand you correctly. 0.0 does not pass as we use it as a string.

Articles  Edit Field   test   Administration(3)

Do you mean this?

@SharkyKZ And we might need this for other validation rules as well.

You're right. If I find a solution here that is good, I would look at more. I think it makes this PR more complicated if I try to includ this here.

avatar SharkyKZ
SharkyKZ - comment - 16 Feb 2020

This issue affects core form validation rules, so not just custom fields but any forms using them.

avatar astridx
astridx - comment - 16 Feb 2020

@SharkyKZ I do not understand what you mean. I know that this rule does not just apply to custom fields.
I prevent a string '0' from being considered as no default value. In my opinion, this is also correct in the core. Don't you see it that way? Can you give me an example of where this is wrong.

avatar ReLater
ReLater - comment - 16 Feb 2020

I haven't tested it but what's about is_numeric ?
https://www.php.net/manual/en/function.is-numeric.php

avatar ReLater
ReLater - comment - 16 Feb 2020
$tests = array(
-0,
0,
0.0,
.0,
'-0',
'0',
'0.0',
'.0',
'',
null
);

foreach ($tests as $element) {
 if (is_numeric($element)) {
  echo gettype($element)  . ':', PHP_EOL;
  echo var_export($element, true) . " is numeric" . PHP_EOL;
 } else {
  echo gettype($element)  . ':', PHP_EOL;
  echo var_export($element, true) . " is NOT numeric", PHP_EOL;
 }
}
exit;
integer:
0 is numeric
integer:
0 is numeric
double:
0.0 is numeric
double:
0.0 is numeric
string:
'-0' is numeric
string:
'0' is numeric
string:
'0.0' is numeric
string:
'.0' is numeric
string:
'' is NOT numeric
NULL:
NULL is NOT numeric
if (empty($val) && !is_numeric($val))
{
 --- DO sth. ---
}
avatar astridx
astridx - comment - 16 Feb 2020

OK. Now I saw it. $value is of type mixed - although I cannot find a place for thus OptionsRule that contains anything other than string.

But I think we can use the empty() function to make it easier. I just updated the branch.

What do you all mean?

avatar SharkyKZ
SharkyKZ - comment - 17 Feb 2020

This doesn't cover negative values. If you want to keep logic this way, use is_numeric() like suggested by @ReLater.

avatar astridx
astridx - comment - 17 Feb 2020

What is not covered?
Signed 0 (-0) is in PHP equal to not signed 0 for float and int: https://stackoverflow.com/questions/31250241/is-minus-negative-zero-equivalent-to-0-in-php?answertab=votes#tab-top
Am I missing something?

avatar SharkyKZ
SharkyKZ - comment - 17 Feb 2020

No, you're right. My bad. This works fine.

avatar ReLater
ReLater - comment - 26 Feb 2020

This test is irrelevant! My fault.

I have tested this item ? unsuccessfully on 3ccf188

Tested like instructed with today's nightly.

Radio Value: 1 => 1

Default Values:
0
0.0
.0
-0

"Save failed with the following error: The default value is invalid."


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

avatar ReLater ReLater - test_item - 26 Feb 2020 - Tested unsuccessfully
avatar ReLater
ReLater - comment - 26 Feb 2020

I have tested this item successfully on 3ccf188

Ah sorry. There shall be the error if 0 entered as default value.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/27618.
avatar ReLater ReLater - test_item - 26 Feb 2020 - Tested successfully
avatar jwaisner
jwaisner - comment - 4 Mar 2020

I have tested this item successfully on 3ccf188


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

avatar jwaisner jwaisner - test_item - 4 Mar 2020 - Tested successfully
avatar jwaisner jwaisner - change - 4 Mar 2020
Status Pending Ready to Commit
avatar jwaisner
jwaisner - comment - 4 Mar 2020

RTC


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

avatar HLeithner HLeithner - change - 4 Mar 2020
Status Ready to Commit Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2020-03-04 20:45:56
Closed_By HLeithner
Labels Added: ?
avatar HLeithner HLeithner - close - 4 Mar 2020
avatar HLeithner HLeithner - merge - 4 Mar 2020
avatar HLeithner
HLeithner - comment - 4 Mar 2020

Thanks

Add a Comment

Login with GitHub to post a comment