User tests: Successful: Unsuccessful:
Pull Request for Issue in the German forum.
Cast a default Custom Field value of 0
to -0
.
0
but add no value with 0
.You see an error if you want to save a custom field with a default value, that is not possible.
Saving is possible.
No
I cast this value because the function empty is used twice: Here and here in the function test
.
Status | New | ⇒ | Pending |
Category | ⇒ | Administration com_fields |
I have tested this item
Strange, I am able to reproduce with v3.9.15 today.
Sorry false alarm. I had applied your PR when testing. Duh! To me, it feels hackish fix.
Labels |
Added:
?
|
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?
Now it is always invalid.
Category | Administration com_fields | ⇒ | Administration com_fields Libraries |
@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):
If I am correct, we have to treat the case string "0" differently.
Requested updates are done.
I have tested this item
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.
@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 doempty()
check.
I hope I understand you correctly. 0.0
does not pass as we use it as a string.
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.
This issue affects core form validation rules, so not just custom fields but any forms using them.
@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.
I haven't tested it but what's about is_numeric
?
https://www.php.net/manual/en/function.is-numeric.php
$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. ---
}
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?
No, you're right. My bad. This works fine.
I have tested this item
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."
I have tested this item
Ah sorry. There shall be the error if 0 entered as default value.
I have tested this item
Status | Pending | ⇒ | Ready to Commit |
RTC
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:
?
|
Thanks
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.