Jump to:

23358 Posts in 18066 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Using the value of a DropDownField in the Frontend

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

Page: 1
Go to End
Author Topic: 1382 Views
  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Using the value of a DropDownField in the Frontend Link to this post

    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.

  • ayyurek
    Avatar
    Community Member
    41 Posts

    Re: Using the value of a DropDownField in the Frontend Link to this post

    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'
    );

  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Re: Using the value of a DropDownField in the Frontend Link to this post

    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?

  • ayyurek
    Avatar
    Community Member
    41 Posts

    Re: Using the value of a DropDownField in the Frontend Link to this post

    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.

  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Re: Using the value of a DropDownField in the Frontend Link to this post

    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

  • ayyurek
    Avatar
    Community Member
    41 Posts

    Re: Using the value of a DropDownField in the Frontend Link to this post

    Hello Mario,

    It's nice to hear that it works

    Cheers, Arda

    1382 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.