silverstripe-framework/forms/NumericField.php

209 lines
3.8 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\DataObject;
/**
* Text input field with validation for numeric values. Supports validating
* the numeric value as to the {@link i18n::get_locale()} value, or an
* overridden locale specific to this field.
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-formattedinput
*/
class NumericField extends TextField {
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_DECIMAL;
/**
2015-04-27 04:34:52 +02:00
* Override locale for this field.
*
* @var string
*/
protected $locale = null;
2015-04-27 04:34:52 +02:00
/**
* @param mixed $value
* @param array $data
*
* @return $this
*
* @throws Zend_Locale_Exception
*/
public function setValue($value, $data = array()) {
require_once "Zend/Locale/Format.php";
// If passing in a non-string number, or a value
2015-04-27 04:34:52 +02:00
// directly from a DataObject then localise this number
if(is_int($value) || is_float($value) || $data instanceof DataObject) {
$locale = new Zend_Locale($this->getLocale());
2015-04-27 04:34:52 +02:00
$this->value = Zend_Locale_Format::toNumber(
$value,
array('locale' => $locale)
);
} else {
$this->value = $this->clean($value);
}
2015-04-27 04:34:52 +02:00
return $this;
}
/**
2015-04-27 04:34:52 +02:00
* In some cases and locales, validation expects non-breaking spaces.
*
* Returns the value, with all spaces replaced with non-breaking spaces.
*
* @param string $input
2015-04-27 04:34:52 +02:00
*
* @return string
*/
protected function clean($input) {
2015-04-27 04:34:52 +02:00
$replacement = html_entity_decode('&nbsp;', null, 'UTF-8');
return str_replace(' ', $replacement, trim($input));
}
2015-04-27 04:34:52 +02:00
/**
2015-04-27 04:34:52 +02:00
* Determine if the current value is a valid number in the current locale.
*
* @return bool
*/
protected function isNumeric() {
require_once "Zend/Locale/Format.php";
2015-04-27 04:34:52 +02:00
$locale = new Zend_Locale($this->getLocale());
2015-04-27 04:34:52 +02:00
return Zend_Locale_Format::isNumber(
$this->clean($this->value),
array('locale' => $locale)
);
}
2015-04-27 04:34:52 +02:00
/**
* {@inheritdoc}
*/
public function Type() {
return 'numeric text';
}
/**
* Validate this field
*
* @param Validator $validator
* @return bool
*/
public function validate($validator) {
if(!$this->value) {
return true;
}
2014-08-15 08:53:05 +02:00
2015-04-27 04:34:52 +02:00
if($this->isNumeric()) {
return true;
}
$validator->validationError(
$this->name,
_t(
2015-04-27 04:34:52 +02:00
'NumericField.VALIDATION',
"'{value}' is not a number, only numbers can be accepted for this field",
array('value' => $this->value)
),
"validation"
);
2015-04-27 04:34:52 +02:00
return false;
}
2014-08-15 08:53:05 +02:00
/**
2015-04-27 04:34:52 +02:00
* Extracts the number value from the localised string value.
*
* @return string
*/
public function dataValue() {
require_once "Zend/Locale/Format.php";
2015-04-27 04:34:52 +02:00
if(!$this->isNumeric()) {
return 0;
}
$locale = new Zend_Locale($this->getLocale());
2015-04-27 04:34:52 +02:00
$number = Zend_Locale_Format::getNumber(
$this->clean($this->value),
array('locale' => $locale)
);
2015-04-27 04:34:52 +02:00
return $number;
}
2015-04-27 04:34:52 +02:00
/**
2015-04-27 04:34:52 +02:00
* Creates a read-only version of the field.
*
* @return NumericField_Readonly
*/
public function performReadonlyTransformation() {
2015-04-27 04:34:52 +02:00
$field = new NumericField_Readonly(
$this->name,
$this->title,
$this->value
);
$field->setForm($this->form);
2015-04-27 04:34:52 +02:00
return $field;
}
/**
2015-04-27 04:34:52 +02:00
* Gets the current locale this field is set to.
*
* @return string
*/
public function getLocale() {
2015-04-27 04:34:52 +02:00
if($this->locale) {
return $this->locale;
}
return i18n::get_locale();
}
/**
2015-04-27 04:34:52 +02:00
* Override the locale for this field.
*
* @param string $locale
2015-04-27 04:34:52 +02:00
*
* @return $this
*/
public function setLocale($locale) {
$this->locale = $locale;
2015-04-27 04:34:52 +02:00
return $this;
}
}
2015-04-27 04:34:52 +02:00
/**
* Readonly version of a numeric field.
*
* @package forms
* @subpackage fields-basic
*/
class NumericField_Readonly extends ReadonlyField {
2015-04-27 04:34:52 +02:00
/**
* @return static
*/
public function performReadonlyTransformation() {
return clone $this;
}
2015-04-27 04:34:52 +02:00
/**
* @return string
*/
public function Value() {
return $this->value ?: '0';
}
public function getValueCast() {
return 'Decimal';
2015-04-27 04:34:52 +02:00
}
}