CodeIgniter Forums
Locale tolerant 'decimal' validation rule - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Locale tolerant 'decimal' validation rule (/showthread.php?tid=90607)



Locale tolerant 'decimal' validation rule - objecttothis - 04-09-2024

Summary: AFAIK the 'decimal' validation rule will only accept period (.) decimal separators, but some locales use the comma (,) as a decimal separator.
Request: modify decimal to take a parameter (either the BCP-47 locale code or the decimal separator char) so that it will properly validate decimals from locales which use another separator (comma is most common,  but Arabic apparently uses U+066B)

PHP Code:
$rules = ['amount_tendered' => 'trim|required|decimal',];
$messages = ['amount_tendered' => lang('Sales.must_enter_numeric')];

if(!
$this->validate($rules$messages))
{
 echo 
"not a decimal";



With it's current implementation this code fails the decimal validation when the locale dictates users enter comma for the decimal separator.

with the proposed modification this code would become

PHP Code:
$rules = ['amount_tendered' => 'trim|required[,]|decimal',]; //or 'trim|required[az-AZ]|decimal'
$messages = ['amount_tendered' => lang('Sales.must_enter_numeric')];

if(!
$this->validate($rules$messages))
{
 echo 
"not a decimal";




I could modify the $_POST variable to convert it, but that's not good practice. Unless I'm missing something, in it's current implementation, I think I need to create a custom rule which converts the value, then runs the decimal validation rule against it. Is this accurate?