Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinglozzer/ff89186fa891411c6449 to your computer and use it in GitHub Desktop.
Save kinglozzer/ff89186fa891411c6449 to your computer and use it in GitHub Desktop.
Injector:
CurrencyField:
class: FixedCurrencyField
<?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