|
|
|
|
| Re: [i18n] How to accept number input in an other format? [message #66890 is a reply to message #66811 ] |
Fri, 05 December 2008 13:45   |
Sujan Messages: 24 Registered: November 2008 Location: Germany, Karlsruhe |
Junior Member |
|
|
Worked perfectly.
<?php
/**
* apValidatorPreis validates a number as a valid German price. It also converts the input value to a float.
*/
class apValidatorPreis extends sfValidatorBase
{
/**
* Configures the current validator.
*/
protected function configure($options = array(), $messages = array())
{
$this->setMessage('invalid', '"%value%" is not a valid price.');
}
/**
* @see sfValidatorBase
*/
protected function doClean($value)
{
$float = str_replace(',', '.', $value);
if (!is_numeric($float))
{
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
$clean = $float;
return $clean;
}
} ?>
Very simple, but does the job.
Now I only have to format the numbers in the form when I want to edit them, but I'm sure there is a solution for this, too.
- Jan
|
|
|
| Re: [i18n] How to accept number input in an other format? [message #66897 is a reply to message #66677 ] |
Fri, 05 December 2008 15:08  |
whalexis Messages: 207 Registered: July 2008 |
Faithful Member |
|
|
Hi,
Instead of using this $float = str_replace(',', '.', $value);
Because it's tied to the culture you choose. You could use something more open like this.
you could use something like
static public function I18nNumberToPhpNumber($number,$culture='en'){
$numberFormatInfo = sfNumberFormatInfo::getInstance($culture);
$number = str_replace($numberFormatInfo->getDecimalSeparator(),'.',$number);
$number = str_replace($numberFormatInfo->getGroupSeparator(),'',$number);
return $number;
}
My solution is not the best but allow to have a number from any culture.
Alexis
|
|
|