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

Using the value of a DropDownField in the Frontend


Reply

6 Posts   1453 Views

Avatar
theGermaniac

4 May 2011 at 11:37pm Community Member, 82 Posts

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

5 May 2011 at 12:19am (Last edited: 5 May 2011 12:25am), Community Member, 41 Posts

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
theGermaniac

5 May 2011 at 1:25am Community Member, 82 Posts

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

7 May 2011 at 4:35am Community Member, 41 Posts

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
theGermaniac

7 May 2011 at 4:37am Community Member, 82 Posts

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

10 May 2011 at 3:15am Community Member, 41 Posts

Hello Mario,

It's nice to hear that it works :)

Cheers, Arda