Last active
August 29, 2015 14:17
-
-
Save kinglozzer/ff89186fa891411c6449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Injector: | |
CurrencyField: | |
class: FixedCurrencyField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FixedCurrencyField extends CurrencyField { | |
/** | |
* @param mixed $val | |
* @return $this | |
*/ | |
public function setValue($val) { | |
if( ! $val) $val = 0.00; | |
$symbol = Config::inst()->get('Currency', 'currency_symbol'); | |
$this->value = $symbol . number_format((double)preg_replace('/[^0-9.\-]/', '', $val), 2); | |
return $this; | |
} | |
/** | |
* @param Vaildator $validator | |
* @return boolean | |
*/ | |
public function validate($validator) { | |
$symbol = Config::inst()->get('Currency', 'currency_symbol'); | |
if(!empty($this->value)) { | |
if(!preg_match( | |
'/^\s*(\-?\\' . $symbol . '?|\\' . $symbol . '\-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/', | |
$this->value | |
)) { | |
$validator->validationError( | |
$this->name, | |
_t('Form.VALIDCURRENCY', "Please enter a valid currency"), | |
"validation", | |
false | |
); | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment