You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My form validation fails sometimes for fields of type number with float values and using decimal steps, even if the float value is valid.
Blueprint example used in the form:
myfield:
type: numberlabel: "Level between -10 and 10 with 1 digit after the decimal point"placeholder: '-1.5'validate:
min: -10.0max: 10.0step: 0.1
Sometimes the validation is ok and sometimes it fails (for example, a value of -1.9 does not pass the valiation while a value of -1.8 is valid)
This issue seems to be a wrong float to int conversion due to floating point error in the validation function.
My suggestion to overcome this probleme is to 'round' the float before testing. Following code modification of the data Validation class is working for me:
--- Validation.php 2023-07-18 18:43:00.000000000 +0200+++ Validation.php 2023-10-25 09:01:16.758049200 +0200@@ -550,7 +550,9 @@
$step = (float)$params['step'];
// Count of how many steps we are above/below the minimum value.
$pos = ($value - $min) / $step;
+ // prevent wrong validation due to floating point precision+ $pos = round($pos, 4);
return is_int(static::filterNumber($pos, $params, $field));
}
If my understanding of the function is right, $pos should be an integer if we want a positiv validation, so the rounding with 1 or more digit after the decimal point should be ok. I choose a rounding with 4 decimal thinking I will be on the safe side.
Tests were done with Grav v1.7.43, which is the last official release as of this writing, on Linux/Apache and on Xammp for Windows with the same issues
PS: I'm not an advanced PHP developper by far and I can't guarantee that this is the best solution, but it works for me on my specific project.
The text was updated successfully, but these errors were encountered:
My form validation fails sometimes for fields of type
number
with float values and using decimal steps, even if the float value is valid.Blueprint example used in the form:
Sometimes the validation is ok and sometimes it fails (for example, a value of -1.9 does not pass the valiation while a value of -1.8 is valid)
This issue seems to be a wrong float to int conversion due to floating point error in the validation function.
My suggestion to overcome this probleme is to 'round' the float before testing. Following code modification of the data Validation class is working for me:
If my understanding of the function is right,
$pos
should be an integer if we want a positiv validation, so the rounding with 1 or more digit after the decimal point should be ok. I choose a rounding with 4 decimal thinking I will be on the safe side.Tests were done with Grav v1.7.43, which is the last official release as of this writing, on Linux/Apache and on Xammp for Windows with the same issues
PS: I'm not an advanced PHP developper by far and I can't guarantee that this is the best solution, but it works for me on my specific project.
The text was updated successfully, but these errors were encountered: