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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Using the value of a DropDownField in the Frontend


Go to End


6 Posts   2782 Views

Avatar
MarioHofer

Community Member, 107 Posts

4 May 2011 at 11:37pm

Hey there,

I'm trying to use the value from a DropDownField, integrated in the backend, in the frontend.

Therefore I created a DataObject named "Industry". This DataObject hast the value "Name" (see code below):

class Industry extends DataObject {
	
	static $db = array (
		'Name' => 'Text'
	);
	
	public function getCMSFields_forPopup() {
		
		return new FieldSet(new TextField('Name'));
		
	}
	
}

In the backend, the DropDownField is integrated the following way:

class ProjectPage extends Page {
	
	static $has_one = array(
		'IndustryID' => 'Industry'
	);
	
	public function getCMSFields() {
		
		$fields = parent::getCMSFields();
		
		$industries = DataObject::get('Industry')->toDropDownMap('ID', 'Name');
		
		$fields->addFieldToTab('Root.Content.Main', new DropdownField('IndustryID', 'Industry', $industries, $this), 'Content');
		
		return $fields;
		
	}
	
}

class ProjectPage_Controller extends Page_Controller {}

Now I've got the problem, that the value of the frontend is not saved when I change it in the backend and the second thing is, that I can't the the name of the choosen Industry through the frontend.

Can anyone please help me? Thanks.

Avatar
ayyurek

Community Member, 41 Posts

5 May 2011 at 12:19am

Edited: 05/05/2011 12:25am

Hi,

You should use,

static $has_one = array(
      'Industry' => 'Industry'
   ); 

The rest of the code seems correct. Don't use the ID field in relation. But inside the getcmsfields, you should add ID as below.

$indobj = DataObject::get("Industry");
$industries = $indobj->toDropDownMap('ID', 'Name');
$fields->addFieldToTab('Root.Content.Main', new DropdownField('IndustryID','Industry', $industries, '', '', 'Select an industry'), 'Content');

Also I think you should have has_many relation defined in the Industry.php

 static $has_many = array(
      'ProjectPages' => 'ProjectPage'
   ); 

Avatar
MarioHofer

Community Member, 107 Posts

5 May 2011 at 1:25am

Ok, thanks ayyurek. The backend saves the value now, but the frontend shows me an 500 error. Any idea?

And how can I access now the value of the drop down through the frontend?

Avatar
ayyurek

Community Member, 41 Posts

7 May 2011 at 4:35am

Hi,

I have no idea about 500 error. About the value, I am not sure but I think $Industry.Name or

<% control Industry %>$Name<% end_control %>
should return the correct value.

Avatar
MarioHofer

Community Member, 107 Posts

7 May 2011 at 4:37am

Yes, I've got that. Works both.

About the 500 error, maybe it was just too much "trying the wrong thing". I did set up a new installation and importet my templates and codes. Works perfect now.

Thanks for your help, Buddy!

Cheers, Mario

Avatar
ayyurek

Community Member, 41 Posts

10 May 2011 at 3:15am

Hello Mario,

It's nice to hear that it works :)

Cheers, Arda