value = '$' . number_format(ereg_replace('[^0-9.]',"",$val), 2); } /** * Overwrite the datavalue before saving to the db ;-) */ function dataValue() { if($this->value){ return ereg_replace('[^0-9.]',"", $this->value); }else{ return 0.00; } } /** * Create a new class for this field */ function performReadonlyTransformation() { return new CurrencyField_Readonly($this->name, $this->title, $this->value); /* $this is-a object and cant be passed as_a string of the first parameter of formfield constructor. return new CurrencyField_Readonly($this); */ } /** * @see http://regexlib.com/REDetails.aspx?regexp_id=126 */ function jsValidation() { $formID = $this->form->FormName(); $jsFunc =<< 0 && !value.match(/^\\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\$/)) { validationError(el,"Please enter a valid currency.","validation",false); return false; } return true; } } }); JS; Requirements::customScript($jsFunc, 'func_validateCurrency'); return "\$('$formID').validateCurrency('$this->name');"; } function validate() { if(!empty ($this->value) && !preg_match('/^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $this->value)) { $validator->validationError($this->name, _t('Form.VALIDCURRENCY', "Please enter a valid currency."), "validation", false); return false; } return true; } } /** * Creates the appropriate readonly field for this class. */ class CurrencyField_Readonly extends ReadonlyField{ /** * overloaded to display the correctly formated value for this datatype */ function Field() { if($this->value){ $val = $this->dontEscape ? ($this->reserveNL?Convert::raw2xml($this->value):$this->value) : Convert::raw2xml($this->value); $val = '$' . number_format(ereg_replace('[^0-9.]',"",$val), 2); }else { $val = '$0.00'; } $valforInput = $this->value ? Convert::raw2att($val) : ""; return "id() . "\">$valname."\" value=\"".$valforInput."\" />"; } /** * This already is a readonly field. */ function performReadonlyTransformation() { return $this; } } ?>