Zend_Validate_Float locale not working with hi_IN locale

此生再无相见时 提交于 2019-12-22 09:38:26

问题


While working with number validation with 'hi_IN' I am facing following problem.

where Zend_Locale_Format::isFloat is working fine for non single digit and any locale

but not working for single digit and locale 'hi_IN'

Source Code : (test cases)

foreach (array('en_GB','en_US', 'lo_LA', 'hi_IN') as $locale) {

    foreach (array('12', '1') as $value) {

        if (!Zend_Locale_Format::isFloat($value, array('locale' => $locale))) {
            echo $value .' ==> '. $locale .' TRUE <br>';
        } else {
            echo $value .' ==> '. $locale .' FALSE<br>';
        }
    }
}

OutPut of above script

12 ==> en_GB  FALSE
1 ==> en_GB   FALSE
12 ==> en_US  FALSE
1 ==> en_US   FALSE
12 ==> lo_LA  FALSE
1 ==> lo_LA   FALSE
12 ==> hi_IN  FALSE
1 ==> hi_IN   TRUE 

So the issue is that how can i fixed this for hi_IN.

Thanks in advance.


回答1:


There is a bug in the management patterns for decimal .
But ZF1 is (to my knowledge) no longer maintained .

After several tests, I can proposes two solutions:

  • The first is to add a '0' before your number. (replace '1' by '01').

  • The second is to modify the Zend/Local/Format.php file.
    In the private static function _getRegexForType replace :

        if (strpos($pattern, ',') !== false) {
            $parts = explode(',', $pattern);
            $count = count($parts);
            foreach($parts as $key => $part) {
                switch ($part) {
                    case '#':
                    case '-#':
                        if ($part[0] == '-') {
                            $regex[$pkey] .= '[' . $symbols['minus'] . '-]{0,1}';
                        } else {
                            $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
                        }
    
                        if (($parts[$key + 1]) == '##0')  {
                            $regex[$pkey] .= '[0-9]{1,3}';
                        } else if (($parts[$key + 1]) == '##') {
                            $regex[$pkey] .= '[0-9]{1,2}';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
                        }
                        break;
                    case '##':
                        if ($parts[$key + 1] == '##0') {
                            $regex[$pkey] .=  '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
                        }
                        break;
                    case '##0':
                        if ($parts[$key - 1] == '##') {
                            $regex[$pkey] .= '[0-9]';
                        } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#')) {
                            $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
                        }
                        break;
                    case '#0':
                        if ($key == 0) {
                            $regex[$pkey] .= '[0-9]*';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
                        }
                        break;
                }
            }
        }
    

By:

        if (strpos($pattern, ',') !== false) {
            $parts = explode(',', $pattern);
            $count = count($parts);
            $flag = false;
            foreach($parts as $key => $part) {
                switch ($part) {
                    case '#':
                    case '-#':
                        if ($part[0] == '-') {
                            $regex[$pkey] .= '[' . $symbols['minus'] . '-]{0,1}';
                        } else {
                            $regex[$pkey] .= '[' . $symbols['plus'] . '+]{0,1}';
                        }

                        if (($parts[$key + 1]) == '##0')  {
                            $regex[$pkey] .= '[0-9]{1,3}';
                        } else if (($parts[$key + 1]) == '##') {
                            $regex[$pkey] .= '[0-9]{1,2}';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 1):"' . $pattern . '"');
                        }
                        break;
                    case '##':
                        if ($parts[$key + 1] == '##0') {
                            $flag = true;
                            $regex[$pkey] .=  '(\\' . $symbols['group'] . '{0,1}[0-9]{2})*';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 2):"' . $pattern . '"');
                        }
                        break;
                    case '##0':
                        if ($parts[$key - 1] == '##' && !$flag) {
                            $regex[$pkey] .= '[0-9]';
                        } else if (($parts[$key - 1] == '#') || ($parts[$key - 1] == '-#') || (($parts[$key - 1] == '##') && $flag)) {
                            $regex[$pkey] .= '(\\' . $symbols['group'] . '{0,1}[0-9]{3})*';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 3):"' . $pattern . '"');
                        }
                        break;
                    case '#0':
                        if ($key == 0) {
                            $regex[$pkey] .= '[0-9]*';
                        } else {
                            throw new Zend_Locale_Exception('Unsupported token for numberformat (Pos 4):"' . $pattern . '"');
                        }
                        break;
                }
            }
        }

I added a $flag variable that can manage the decimal form of the '#,##,##0.###'
I did some tests and it looks run.

Not being an expert in the regulars expressions don't hesitate to send me a return. :)



来源:https://stackoverflow.com/questions/23195934/zend-validate-float-locale-not-working-with-hi-in-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!