Using Zend Validate Float with a specific locale
$text = new Zend_Form_Element_Text();
$ValidateRange = new Zend_Validate_Between(0, 999999.99);
$ValidateFloat = new Zend_Validate_Float();
$ValidateFloat->setLocale(new Zend_Locale('de_AT'))开发者_如何学编程;
$ValidateRange->setMessage('Please enter the amount between [0-9999] ');
$textValidateFloat->setMessage('Please enter the amount properly');
$text->addValidator($ValidateRange);
$text->addValidator($ValidateFloat);
The above code works fine if we enter value like 12,23 . If we enter 12.23 the form didn't show any error message . How we can show error message . Please help me . thanks in advance...
Looking in the Zend/Validate/Float code, it always checks both your defined locale and english:
if (!Zend_Locale_Format::isFloat($value, array('locale' => 'en')) &&
!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) {
So, you'll need to extend or rewrite this validator yourself, removing the 'locale'=>'en' part.
edit:
Your validator, extending Zend_Validate_Float, can look something like this:
public function isValid( $value )
{
if ( !parent::isValid($value) ) return false;
if (!Zend_Locale_Format::isFloat($value, array('locale' => 'de_AT'))) {
$this->_error(self::NOT_FLOAT);
return false;
}
return true;
}
精彩评论