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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

CountryDropdownField Default Country


Reply

4 Posts   2151 Views

Avatar
Jonn

30 December 2010 at 4:15pm Community Member, 10 Posts

Hi,

I'm trying to build a custom form for a page with a country dropdown and I can't get it to show the country I want selected rather than just the first entry.

new CountryDropdownField(
$name = 'Country',
$title = 'Country',
$value = 'US'
)

I noticed the the API I can call $isSelected but I not sure where/how to implement this. The userforms module now has a country dropdown but again I cant see where to set the default country.

Thanks

Avatar
Willr

1 January 2011 at 6:14pm Forum Moderator, 5511 Posts

Having a quick look at the API for the field the constructor states that the default value is the 4th argument rather than the 3rd which could be the solution. You may also want to disable the defaultToVisitorCountry check if GeoIP doesn't work.

http://api.silverstripe.org/2.4/forms/fields-relational/CountryDropdownField.html

Avatar
Jonn

4 January 2011 at 1:20pm Community Member, 10 Posts

Thanks Willr, I got both my custom forms and userforms working correctly now.

Avatar
datswicked

8 March 2013 at 11:11am Community Member, 15 Posts

Just found this post and have done this in SS3.0.4 as below, might be useful for others..

static $db = array(
'BirthPlace' => 'Varchar(255)'
);
static $default_country = 'AU';

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main',new CountryDropdownField('BirthPlace', 'Birth Place'));
return $fields;
}