Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Currency, CurrencySymbol, CurrencyField, £, $, GBP - EXPLAINED!


Go to End


5 Posts   5137 Views

Avatar
CHD

Community Member, 219 Posts

21 September 2012 at 12:20am

Edited: 21/09/2012 12:20am

So today I pulled my hair out for an hour trying to do somehting as simple as display £ signs in the CMS.
I've had this issue before with the 2.x versions of SS (not sure if its sorted in 3?) and usually you have to hack the Currency.php file which has

protected static $currencySymbol = '$';

why this is hardcoded in I'll never understand.

the problem is changing the $ to £ doesnt update the actual CMS fields, so you'll still see dollars all the time.
I tried updating CurrencyField.php as that too has dollar signs hardcoded in

function setValue($val) {
		$value = ($val) ? $val : 0.00;
		$this->value = '$' . number_format((double)preg_replace('/[^0-9.\-]/', '', $value), 2);
	}

So naturally I tried changing the $ to a £. which showed the "?" in the CMS as an unrecognised symbol.
Changing it to £ just displayed the curreny as £50.00
So I had to (In Dreamweaver) go to Modify>Page Properties>Title/Encoding and change to UTF-8.

and that fixed it.

I hate hackign core files, they'll only get overwritten at some point, but sometimes SS really seems to leave you no choice.

Anyway, hopefully this helps.

Avatar
CHD

Community Member, 219 Posts

21 September 2012 at 12:42am

Edited: 21/09/2012 12:43am

P.S - You'll also find the fields will now throw up JS validation errors, because the stupid thing is hardcoded to look for $ signs, if there are none, it's not allowed.

I checked regexpress for a GBP validator, but couldnt find one, so i've just ripped the JS validator function now as i'm simply too sick of this to carry on.

I'm 100% sure that what I've done is a load of unnecessary crap, so PLEASE if somebody knows a simpler way to handle non $ currency/money in a basic SS install without the eCommerce module, let us all know!

cheers

Avatar
Willr

Forum Moderator, 5523 Posts

22 September 2012 at 4:57pm

Patches welcome! Obviously a legacy thing, no ones needed to update all the currencies framework wide (though sooner or later someone will!) Every reference throughout needs to be updated to use that variable you pointed out rather than hard coded string. There already exists a Currency::setCurrencySymbol(); function to override it globally but there doesn't appear to be a getter which is perhaps why it's not used. Raise it as a ticket (if it isn't already) on open.silverstripe.org and if you can list all the road blocks you're running that will help it get fixed for the next version.

Avatar
merrick_sd

Community Member, 99 Posts

8 January 2014 at 3:21am

in my config.php i've set
Currency::setCurrencySymbol('£');

and on the front end this works
$Price.Nice

However in my admin area its show a $

public static $db = array(
'Price' => 'Currency');

public function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Main', new CurrencyField("Price", "Price"));
return $fields;

}

Avatar
aktorou

Community Member, 2 Posts

4 October 2015 at 11:15am

Incase anyone else is still looking for a solution to this, here's mine (tested using silverstripe 3.1)
Create a new class that extends the CurrencyField:

class LocalCurrencyField extends CurrencyField{

  private $currencyHolder = '$';

  public function setMyLocalCurrency(){
    $this->currencyHolder = Currency::config()->currency_symbol;
  }

  public function setValue($val) {
    $this->setMyLocalCurrency();s
    if(!$val) $val = 0.00;
    $this->value = $this->currencyHolder . number_format((double)preg_replace('/[^0-9.\-]/', '', $val), 2);
    return $this;
  }

  public function validate($validator) {
    if(!empty ($this->value)
        && !preg_match('/^\s*(\-?\\'.$this->currencyHolder.'?|\\'.$this->currencyHolder.'\-?)?(\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;
  }

}

Set the currency in _config.php file using:

Currency::setCurrencySymbol('£');

Then instead of using CurrencyField::create() or new CurrencyField() use:

LocalCurrencyField::create()