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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

How to use a text field to create a select list (both in beckend)?


Go to End


2 Posts   1132 Views

Avatar
MarioSommereder

Community Member, 107 Posts

27 April 2011 at 1:10am

Hi,

I want to create a select list with data taken from a text field. The text field is listed in the backend and so is the select list.

The text field should be something like "Value 1,Value XY, Another Value" (separated by comma) and is located at the page holder. The pages underneath this holder should show the select list with the values taken from the before mentioned text field.

With what I don't have problems is creating the single elements. My problem ist to parse the text field values to the select list.

Any help appreciated!

Cheers, Mario

Avatar
Devlin

Community Member, 344 Posts

27 April 2011 at 8:58pm

Edited: 28/04/2011 1:43am

http://php.net/manual/de/function.explode.php

You should also consider using a simple DataObject for this, so if anyone deletes values in the TextField your keys won't get mixed up.

Something like:

class SelectModelAdmin extends ModelAdmin {
	public static $managed_models = array(
		'SelectModel'
	);
	static $url_segment = 'selectadmin';
	static $menu_title = 'Select Admin';
}
class SelectModel extends DataObject {
	static $db = array('Name' => 'Text');
	static $searchable_fields = array('Name');
}
class Page extends SiteTree {
	static $has_one = array(
		'Select' => 'SelectModel',
	);
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$select = new DropdownField('SelectID', 'Select', DataObject::get('SelectModel')->map('ID','Name'));
		$fields->addFieldToTab('Root.Content.Main', $select, 'Content');
		return $fields;
	}
}