Welcome Guest, Not a member yet? Register   Sign In
Locale tolerant 'decimal' validation rule
#1

(This post was last modified: 04-09-2024, 06:18 AM by objecttothis.)

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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB