Version: current

      1 <?php
      2 /**
      3  * Currency field.
      4  *
      5  * @todo Add localization support, see http://open.silverstripe.com/ticket/2931
      6  *
      7  * @package forms
      8  * @subpackage fields-formattedinput
      9  */
     10 class CurrencyField extends TextField {
     11     /**
     12      * allows the value to be set ( not including $ signs and number format...)
     13      */
     14     function setValue($val) {
     15         $value = ($val) ? $val : 0.00;
     16         $this->value = '$' . number_format(ereg_replace('[^0-9.]', '', $value), 2);
     17     }
     18     /**
     19      * Overwrite the datavalue before saving to the db ;-)
     20      */
     21     function dataValue() {
     22         if($this->value){
     23             return preg_replace('/[^0-9.]/',"", $this->value);
     24         }else{
     25             return 0.00;
     26         }
     27     }
     28     /**
     29      * Create a new class for this field
     30      */
     31     function performReadonlyTransformation() {
     32
     33         $field = new CurrencyField_Readonly($this->name, $this->title, $this->value);
     34         $field -> addExtraClass($this->extraClass());
     35         return $field;
     36
     37         /*
     38         $this is-a object and cant be passed as_a string of the first parameter of formfield constructor.
     39         return new CurrencyField_Readonly($this);
     40         */
     41     }
     42
     43     /**
     44      * @see http://regexlib.com/REDetails.aspx?regexp_id=126
     45      */
     46     function jsValidation() {
     47         $formID = $this->form->FormName();
     48         $error = _t('CurrencyField.VALIDATIONJS', 'Please enter a valid currency.');
     49         $jsFunc =<<<JS
     50 Behaviour.register({
     51     "#$formID": {
     52         validateCurrency: function(fieldName) {
     53             var el = _CURRENT_FORM.elements[fieldName];
     54             if(!el || !el.value) return true;
     55
     56             var value = \$F(el);
     57             if(value.length > 0 && !value.match(/^\s*\\\\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*\$/)) {
     58                 validationError(el,"$error","validation",false);
     59                 return false;
     60             }
     61             return true;
     62         }
     63     }
     64 });
     65 JS;
     66
     67         Requirements::customScript($jsFunc, 'func_validateCurrency_' .$formID);
     68
     69         return <<<JS
     70         if(\$('$formID')) \$('$formID').validateCurrency('$this->name');
     71 JS;
     72     }
     73
     74     function validate($validator) {
     75         if(!empty ($this->value) && !preg_match('/^\s*\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/', $this->value)) {
     76             $validator->validationError($this->name, _t('Form.VALIDCURRENCY', "Please enter a valid currency."), "validation", false);
     77             return false;
     78         }
     79         return true;
     80     }
     81 }
     82
     83 /**
     84  * Readonly version of a {@link CurrencyField}.
     85  * @package forms
     86  * @subpackage fields-formattedinput
     87  */
     88 class CurrencyField_Readonly extends ReadonlyField{
     89
     90     /**
     91      * overloaded to display the correctly formated value for this datatype
     92      */
     93     function Field() {
     94         if($this->value){
     95             $val = $this->dontEscape ? ($this->reserveNL?Convert::raw2xml($this->value):$this->value) : Convert::raw2xml($this->value);
     96             $val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
     97
     98         }else {
     99                 $val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
    100         }
    101         $valforInput = $this->value ? Convert::raw2att($val) : "";
    102         return "<span class=\"readonly ".$this->extraClass()."\" id=\"" . $this->id() . "\">$val</span><input type=\"hidden\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
    103     }
    104     /**
    105      * This already is a readonly field.
    106      */
    107     function performReadonlyTransformation() {
    108         return clone $this;
    109     }
    110
    111 }
    112
    113 /**
    114  * Readonly version of a {@link CurrencyField}.
    115  * @package forms
    116  * @subpackage fields-formattedinput
    117  */
    118 class CurrencyField_Disabled extends CurrencyField{
    119
    120     protected $disabled = true;
    121
    122     /**
    123      * overloaded to display the correctly formated value for this datatype
    124      */
    125     function Field() {
    126         if($this->value){
    127             $val = $this->dontEscape ? ($this->reserveNL?Convert::raw2xml($this->value):$this->value) : Convert::raw2xml($this->value);
    128             $val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
    129
    130         }else {
    131                 $val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
    132         }
    133         $valforInput = $this->value ? Convert::raw2att($val) : "";
    134         return "<input class=\"text\" type=\"text\" disabled=\"disabled\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
    135     }
    136 }
    137
    138 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -