Skip to content

Conversation

@astridx
Copy link
Contributor

@astridx astridx commented Jan 23, 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.

@N6REJ
Copy link
Contributor

N6REJ commented Jan 27, 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.

1 similar comment
@jwaisner
Copy link
Member

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.

@Quy
Copy link
Contributor

Quy commented Jan 30, 2020

Not able to reproduce with v3.9.15.

27618

@astridx
Copy link
Contributor Author

astridx commented Jan 31, 2020

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

@Quy
Copy link
Contributor

Quy commented Feb 4, 2020

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

@astridx
Copy link
Contributor Author

astridx commented Feb 4, 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?

@Quy
Copy link
Contributor

Quy commented Feb 5, 2020

Now it is always invalid.

@astridx
Copy link
Contributor Author

astridx commented Feb 8, 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.

@astridx
Copy link
Contributor Author

astridx commented Feb 8, 2020

@N6REJ I would be happy if you could test again

$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');

if (!$required && empty($value))
if (!$required && empty($value) && !($data['default_value'] !== "0"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$data is undefined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, corrected

@astridx
Copy link
Contributor Author

astridx commented Feb 12, 2020

Requested updates are done.

@Quy
Copy link
Contributor

Quy commented Feb 13, 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.

@SharkyKZ
Copy link
Contributor

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.

@astridx
Copy link
Contributor Author

astridx commented Feb 14, 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.

@SharkyKZ
Copy link
Contributor

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

@astridx
Copy link
Contributor Author

astridx commented Feb 16, 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.

@ReLater
Copy link
Contributor

ReLater commented Feb 16, 2020

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

@ReLater
Copy link
Contributor

ReLater commented Feb 16, 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. ---
}

@astridx
Copy link
Contributor Author

astridx commented Feb 16, 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?

@SharkyKZ
Copy link
Contributor

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

@astridx
Copy link
Contributor Author

astridx commented Feb 17, 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?

@SharkyKZ
Copy link
Contributor

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

@ReLater
Copy link
Contributor

ReLater commented Feb 26, 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.

@ReLater
Copy link
Contributor

ReLater commented Feb 26, 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.

@jwaisner
Copy link
Member

jwaisner commented Mar 4, 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.

@jwaisner
Copy link
Member

jwaisner commented Mar 4, 2020

RTC


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

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Mar 4, 2020
@HLeithner HLeithner merged commit fcd1885 into joomla:staging Mar 4, 2020
@HLeithner
Copy link
Member

Thanks

@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Mar 4, 2020
@HLeithner HLeithner added this to the Joomla! 3.9.16 milestone Mar 4, 2020
@astridx astridx deleted the defaultvalue branch August 9, 2020 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants