2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A Single Numeric field extending a typical
|
|
|
|
* TextField but with validation.
|
|
|
|
*/
|
|
|
|
class NumericField extends TextField{
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
|
|
|
|
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateNumericField: function(fieldName) {
|
|
|
|
el = _CURRENT_FORM.elements[fieldName];
|
|
|
|
if(!el || !el.value) return true;
|
|
|
|
|
2007-08-23 07:47:54 +02:00
|
|
|
if(el.value.match(/^([0-9]+(\.[0-9]+)?$)/)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
validationError(el, "'" + el.value + "' is not a number, only numbers can be accepted for this field","validation");
|
|
|
|
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){
|
|
|
|
if($this->value && !is_numeric($this->value)){
|
|
|
|
$validator->validationError($this->name,"'$this->value' is not a number, only numbers can be accepted for this field","validation");
|
|
|
|
return false;
|
|
|
|
} else{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataValue() {
|
|
|
|
return (is_numeric($this->value)) ? $this->value : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|