2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-11 12:51:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2013-05-11 12:51:39 +02:00
|
|
|
* Text input field with validation for numeric values. Supports validating
|
|
|
|
* the numeric value as to the {@link i18n::get_locale()} value.
|
2012-06-26 15:03:11 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-05-11 12:51:39 +02:00
|
|
|
class NumericField extends TextField {
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return 'numeric text';
|
|
|
|
}
|
2012-04-11 05:55:07 +02:00
|
|
|
|
2013-05-11 12:51:39 +02:00
|
|
|
public function validate($validator) {
|
|
|
|
if(!$this->value && !$validator->fieldIsRequired($this->name)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-13 19:55:05 +02:00
|
|
|
|
|
|
|
require_once THIRDPARTY_PATH."/Zend/Locale/Format.php";
|
2013-05-11 12:51:39 +02:00
|
|
|
|
|
|
|
$valid = Zend_Locale_Format::isNumber(
|
|
|
|
trim($this->value),
|
|
|
|
array('locale' => i18n::get_locale())
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!$valid) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2012-05-01 21:44:54 +02:00
|
|
|
_t(
|
|
|
|
'NumericField.VALIDATION', "'{value}' is not a number, only numbers can be accepted for this field",
|
|
|
|
array('value' => $this->value)
|
2007-10-25 04:47:45 +02:00
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
2013-05-11 12:51:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-11 12:51:39 +02:00
|
|
|
|
|
|
|
return true;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataValue() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return (is_numeric($this->value)) ? $this->value : 0;
|
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a readonly version of this field
|
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
$field = new NumericField_Readonly($this->name, $this->title, $this->value);
|
|
|
|
$field->setForm($this->form);
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-03 03:55:23 +01:00
|
|
|
|
|
|
|
class NumericField_Readonly extends ReadonlyField{
|
|
|
|
|
|
|
|
public function performReadonlyTransformation() {
|
|
|
|
return clone $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Value() {
|
|
|
|
return Convert::raw2xml($this->value ?
|
|
|
|
"$this->value" : "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|