mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-30 05:09:06 +02:00
Clean up NumericField
This commit is contained in:
parent
0653ba9630
commit
47ae8ac7e9
@ -4,138 +4,199 @@
|
|||||||
* Text input field with validation for numeric values. Supports validating
|
* Text input field with validation for numeric values. Supports validating
|
||||||
* the numeric value as to the {@link i18n::get_locale()} value, or an
|
* the numeric value as to the {@link i18n::get_locale()} value, or an
|
||||||
* overridden locale specific to this field.
|
* overridden locale specific to this field.
|
||||||
*
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
* @subpackage fields-formattedinput
|
* @subpackage fields-formattedinput
|
||||||
*/
|
*/
|
||||||
class NumericField extends TextField {
|
class NumericField extends TextField {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override locale for this field
|
* Override locale for this field.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $locale = null;
|
protected $locale = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $value
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* @throws Zend_Locale_Exception
|
||||||
|
*/
|
||||||
public function setValue($value, $data = array()) {
|
public function setValue($value, $data = array()) {
|
||||||
require_once "Zend/Locale/Format.php";
|
require_once "Zend/Locale/Format.php";
|
||||||
|
|
||||||
// If passing in a non-string number, or a value
|
// If passing in a non-string number, or a value
|
||||||
// directly from a dataobject then localise this number
|
// directly from a DataObject then localise this number
|
||||||
if ((is_numeric($value) && !is_string($value)) ||
|
|
||||||
($value && $data instanceof DataObject)
|
if(is_int($value) || is_float($value) || $data instanceof DataObject) {
|
||||||
){
|
|
||||||
$locale = new Zend_Locale($this->getLocale());
|
$locale = new Zend_Locale($this->getLocale());
|
||||||
$this->value = Zend_Locale_Format::toNumber($value, array('locale' => $locale));
|
|
||||||
|
$this->value = Zend_Locale_Format::toNumber(
|
||||||
|
$value,
|
||||||
|
array('locale' => $locale)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// If an invalid number, store it anyway, but validate() will fail
|
|
||||||
$this->value = $this->clean($value);
|
$this->value = $this->clean($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In some cases and locales, validation expects non-breaking spaces
|
* In some cases and locales, validation expects non-breaking spaces.
|
||||||
|
*
|
||||||
|
* Returns the value, with all spaces replaced with non-breaking spaces.
|
||||||
*
|
*
|
||||||
* @param string $input
|
* @param string $input
|
||||||
* @return string The input value, with all spaces replaced with non-breaking spaces
|
*
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function clean($input) {
|
protected function clean($input) {
|
||||||
$nbsp = html_entity_decode(' ', null, 'UTF-8');
|
$replacement = html_entity_decode(' ', null, 'UTF-8');
|
||||||
return str_replace(' ', $nbsp, trim($input));
|
|
||||||
|
return str_replace(' ', $replacement, trim($input));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the current value is a valid number in the current locale
|
* Determine if the current value is a valid number in the current locale.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function isNumeric() {
|
protected function isNumeric() {
|
||||||
require_once "Zend/Locale/Format.php";
|
require_once "Zend/Locale/Format.php";
|
||||||
|
|
||||||
$locale = new Zend_Locale($this->getLocale());
|
$locale = new Zend_Locale($this->getLocale());
|
||||||
|
|
||||||
return Zend_Locale_Format::isNumber(
|
return Zend_Locale_Format::isNumber(
|
||||||
$this->clean($this->value),
|
$this->clean($this->value),
|
||||||
array('locale' => $locale)
|
array('locale' => $locale)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function Type() {
|
public function Type() {
|
||||||
return 'numeric text';
|
return 'numeric text';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Validator $validator
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public function validate($validator) {
|
public function validate($validator) {
|
||||||
if(!$this->value && !$validator->fieldIsRequired($this->name)) {
|
if(!$this->value && !$validator->fieldIsRequired($this->name)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->isNumeric()) return true;
|
if($this->isNumeric()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$validator->validationError(
|
$validator->validationError(
|
||||||
$this->name,
|
$this->name,
|
||||||
_t(
|
_t(
|
||||||
'NumericField.VALIDATION', "'{value}' is not a number, only numbers can be accepted for this field",
|
'NumericField.VALIDATION',
|
||||||
|
"'{value}' is not a number, only numbers can be accepted for this field",
|
||||||
array('value' => $this->value)
|
array('value' => $this->value)
|
||||||
),
|
),
|
||||||
"validation"
|
"validation"
|
||||||
);
|
);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the number value from the localised string value
|
* Extracts the number value from the localised string value.
|
||||||
*
|
*
|
||||||
* @return string number value
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function dataValue() {
|
public function dataValue() {
|
||||||
require_once "Zend/Locale/Format.php";
|
require_once "Zend/Locale/Format.php";
|
||||||
if(!$this->isNumeric()) return 0;
|
|
||||||
|
if(!$this->isNumeric()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
$locale = new Zend_Locale($this->getLocale());
|
$locale = new Zend_Locale($this->getLocale());
|
||||||
|
|
||||||
$number = Zend_Locale_Format::getNumber(
|
$number = Zend_Locale_Format::getNumber(
|
||||||
$this->clean($this->value),
|
$this->clean($this->value),
|
||||||
array('locale' => $locale)
|
array('locale' => $locale)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $number;
|
return $number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a readonly version of this field
|
* Creates a read-only version of the field.
|
||||||
|
*
|
||||||
|
* @return NumericField_Readonly
|
||||||
*/
|
*/
|
||||||
public function performReadonlyTransformation() {
|
public function performReadonlyTransformation() {
|
||||||
$field = new NumericField_Readonly($this->name, $this->title, $this->value);
|
$field = new NumericField_Readonly(
|
||||||
|
$this->name,
|
||||||
|
$this->title,
|
||||||
|
$this->value
|
||||||
|
);
|
||||||
|
|
||||||
$field->setForm($this->form);
|
$field->setForm($this->form);
|
||||||
|
|
||||||
return $field;
|
return $field;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current locale this field is set to
|
* Gets the current locale this field is set to.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getLocale() {
|
public function getLocale() {
|
||||||
return $this->locale ?: i18n::get_locale();
|
if($this->locale) {
|
||||||
|
return $this->locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i18n::get_locale();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override the locale for this field
|
* Override the locale for this field.
|
||||||
*
|
*
|
||||||
* @param string $locale
|
* @param string $locale
|
||||||
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setLocale($locale) {
|
public function setLocale($locale) {
|
||||||
$this->locale = $locale;
|
$this->locale = $locale;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Readonly version of a numeric field.
|
||||||
|
*
|
||||||
|
* @package forms
|
||||||
|
* @subpackage fields-basic
|
||||||
|
*/
|
||||||
class NumericField_Readonly extends ReadonlyField {
|
class NumericField_Readonly extends ReadonlyField {
|
||||||
|
/**
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
public function performReadonlyTransformation() {
|
public function performReadonlyTransformation() {
|
||||||
return clone $this;
|
return clone $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function Value() {
|
public function Value() {
|
||||||
return Convert::raw2xml($this->value ? "$this->value" : "0");
|
if($this->value) {
|
||||||
}
|
return Convert::raw2xml((string) $this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return '0';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user