21278 Posts in 5728 Topics by 2599 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 812 Views |
-
Using the value of a DropDownField in the Frontend

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.
-
Re: Using the value of a DropDownField in the Frontend

5 May 2011 at 12:19am Last edited: 5 May 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'
); -
Re: Using the value of a DropDownField in the Frontend

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?
-
Re: Using the value of a DropDownField in the Frontend

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
should return the correct value.<% control Industry %>$Name<% end_control %>
-
Re: Using the value of a DropDownField in the Frontend

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
-
Re: Using the value of a DropDownField in the Frontend

10 May 2011 at 3:15am
Hello Mario,
It's nice to hear that it works
Cheers, Arda
| 812 Views | ||
|
Page:
1
|
Go to Top |


