2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A Single Numeric field extending a typical
|
|
|
|
* TextField but with validation.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class NumericField extends TextField{
|
|
|
|
|
2009-07-17 01:58:42 +02:00
|
|
|
function Field() {
|
|
|
|
$html = parent::Field();
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . 'javascript/NumericField.js');
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
2008-01-10 04:28:13 +01:00
|
|
|
$error = _t('NumericField.VALIDATIONJS', 'is not a number, only numbers can be accepted for this field');
|
2007-07-19 12:40:28 +02:00
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateNumericField: function(fieldName) {
|
|
|
|
el = _CURRENT_FORM.elements[fieldName];
|
|
|
|
if(!el || !el.value) return true;
|
|
|
|
|
2009-04-29 01:55:53 +02:00
|
|
|
if(el.value.match(/^\s*([0-9]+(\.[0-9]+)?\s*$)/)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(el, "'" + el.value + "' $error","validation");
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
|
|
|
|
Requirements::customScript($jsFunc, 'func_validateNumericField');
|
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
//return "\$('$formID').validateNumericField('$this->name');";
|
|
|
|
return <<<JS
|
|
|
|
if(typeof fromAnOnBlur != 'undefined'){
|
|
|
|
if(fromAnOnBlur.name == '$this->name')
|
|
|
|
$('$formID').validateNumericField('$this->name');
|
|
|
|
}else{
|
|
|
|
$('$formID').validateNumericField('$this->name');
|
|
|
|
}
|
|
|
|
JS;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** PHP Validation **/
|
|
|
|
function validate($validator){
|
2009-04-29 01:55:53 +02:00
|
|
|
if($this->value && !is_numeric(trim($this->value))){
|
2007-10-25 04:47:45 +02:00
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
sprintf(
|
|
|
|
_t('NumericField.VALIDATION', "'%s' is not a number, only numbers can be accepted for this field"),
|
|
|
|
$this->value
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
} else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataValue() {
|
|
|
|
return (is_numeric($this->value)) ? $this->value : 0;
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|