This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.1 » [i18n] How to accept number input in an other format?
[i18n] How to accept number input in an other format? [message #66677] Wed, 03 December 2008 18:49 Go to next message
Sujan  is currently offline Sujan
Messages: 24
Registered: November 2008
Location: Germany, Karlsruhe
Junior Member
Hi,

I am working on an application with default_culture 'de_DE'. Outputting currency data
works fine with format_number(), '19.99' from the database is output as '19,99'. Perfect.

But what do I have to do that I can input '19,99' instead of '19.99' in my forms and it
gets converted automatically?

I am using sfValidatorNumber on these fields at the moment, so I get '"19,99" is not
a number.' error message Sad

- Jan
Re: [i18n] How to accept number input in an other format? [message #66709 is a reply to message #66677 ] Thu, 04 December 2008 01:38 Go to previous messageGo to next message
martinopiccinato  is currently offline martinopiccinato
Messages: 7
Registered: November 2008
Junior Member
I suggest you to write your own DE (or i18ned) validator because sfValidatorNumber is simply using PHP is_numeric function that returns true or false depending on PHP locale (probably en_US on your host). You might change it with set_locale php function just before validating but this would affect everything.

Re: [i18n] How to accept number input in an other format? [message #66731 is a reply to message #66709 ] Thu, 04 December 2008 10:37 Go to previous messageGo to next message
Sujan  is currently offline Sujan
Messages: 24
Registered: November 2008
Location: Germany, Karlsruhe
Junior Member
Thanks martinopiccinato. Will try that.

Probably a silly question, but where do I place the file that
contains my validator?

My database only accepts '19.99', is doClean() in the validator
the right place to replace ',' with '.'?

- Jan
Re: [i18n] How to accept number input in an other format? [message #66811 is a reply to message #66677 ] Thu, 04 December 2008 22:23 Go to previous messageGo to next message
martinopiccinato  is currently offline martinopiccinato
Messages: 7
Registered: November 2008
Junior Member
you can place it in lib/forms (or if you prefer you can create a lib/forms/validators if you have many).

Yes, I think doClean would be an appropriate point.
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 Go to previous messageGo to next message
Sujan  is currently offline 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 Go to previous message
whalexis  is currently offline 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
Previous Topic:Generate link between apps (backend to frontend)
Next Topic:Help with defining a Criteria object
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software