mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
a99dbae012
Fixes http://open.silverstripe.org/ticket/5577. Uses Zend_Locale_Format::isNumber(). Includes unit test for NumericField. Does not include testing work on DBField underlying NumericField to ensure that works consistently.
46 lines
932 B
PHP
46 lines
932 B
PHP
<?php
|
|
|
|
/**
|
|
* Text input field with validation for numeric values. Supports validating
|
|
* the numeric value as to the {@link i18n::get_locale()} value.
|
|
*
|
|
* @package forms
|
|
* @subpackage fields-formattedinput
|
|
*/
|
|
class NumericField extends TextField {
|
|
|
|
public function Type() {
|
|
return 'numeric text';
|
|
}
|
|
|
|
public function validate($validator) {
|
|
if(!$this->value && !$validator->fieldIsRequired($this->name)) {
|
|
return true;
|
|
}
|
|
|
|
$valid = Zend_Locale_Format::isNumber(
|
|
trim($this->value),
|
|
array('locale' => i18n::get_locale())
|
|
);
|
|
|
|
if(!$valid) {
|
|
$validator->validationError(
|
|
$this->name,
|
|
_t(
|
|
'NumericField.VALIDATION', "'{value}' is not a number, only numbers can be accepted for this field",
|
|
array('value' => $this->value)
|
|
),
|
|
"validation"
|
|
);
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function dataValue() {
|
|
return (is_numeric($this->value)) ? $this->value : 0;
|
|
}
|
|
}
|