Class DropdownField
Dropdown field, created from a <select> tag.
Setting a $has_one relation
Using here an example of an art gallery, with Exhibition pages, each of which has a Gallery they belong to. The Gallery class is also user-defined.
static $has_one = array( 'Gallery' => 'Gallery', ); public function getCMSFields() { $fields = parent::getCMSFields(); $field = new DropdownField('GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title')); $field->setEmptyString('(Select one)'); $fields->addFieldToTab('Root.Content', $field, 'Content');
As you see, you need to put "GalleryID", rather than "Gallery" here.
Populate with Array
Example model defintion:
class MyObject extends DataObject { static $db = array( 'Country' => "Varchar(100)" ); }
Example instantiation:
new DropdownField( 'Country', 'Country', array( 'NZ' => 'New Zealand', 'US' => 'United States' 'GEM'=> 'Germany' ) );
Populate with Enum-Values
You can automatically create a map of possible values from an Enum
database column.
Example model definition:
class MyObject extends DataObject { static $db = array( 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')" ); }
Field construction:
new DropdownField( 'Country', 'Country', singleton('MyObject')->dbObject('Country')->enumValues() );
- Object
-
ViewableData
implements
IteratorAggregate
-
RequestHandler
-
FormField
-
DropdownField
Direct known subclasses
CountryDropdownField,
GroupedDropdownField,
ListboxField,
LookupField,
OptionsetField
Indirect known subclasses
See: CheckboxSetField for multiple selections through checkboxes instead.
See: ListboxField for a single <select> box (with single or multiple selections).
See: TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
Used by:
ForeignKey Located at sapphire/forms/DropdownField.php
Methods summary
public
|
#
__construct( mixed $name, mixed $title = null, mixed $source = array(), mixed $value = '', mixed $form = null, mixed $emptyString = null )
Creates a new dropdown field. |
public
string
|
#
Field( array $properties = array() )
Returns the form field - used by templates. Although FieldHolder is generally what is inserted into templates, all of the field holder templates make use of $Field. It's expected that FieldHolder will give you the "complete" representation of the field on the form, whereas Field will give you the core editing widget, such as an input tag. |
public
array
|
|
public
boolean
|
|
public
array
|
|
public
|
|
public
|
|
public
boolean
|
|
public
|
#
setEmptyString( string $str )
Set the default selection label, e.g. "select...". Defaults to an empty
string. Automatically sets |
public
string
|
|
public
|
Methods inherited from FormField
FieldHolder(),
ID(),
LeftTitle(),
Link(),
Message(),
MessageType(),
Name(),
Required(),
RightTitle(),
SmallFieldHolder(),
Title(),
Type(),
Value(),
addExtraClass(),
attrTitle(),
attrValue(),
createTag(),
dataValue(),
debug(),
describe(),
extraClass(),
forTemplate(),
getAttribute(),
getAttributesHTML(),
getCustomValidationMessage(),
getDescription(),
getFieldHolderTemplate(),
getFieldHolderTemplates(),
getForm(),
getName(),
getSmallFieldHolderTemplate(),
getSmallFieldHolderTemplates(),
getTabIndex(),
getTemplate(),
getTemplates(),
hasClass(),
hasData(),
isComposite(),
isDisabled(),
isReadonly(),
name_to_label(),
performDisabledTransformation(),
removeExtraClass(),
rootFieldList(),
rootFieldSet(),
saveInto(),
securityTokenEnabled(),
setAttribute(),
setContainerFieldList(),
setContainerFieldSet(),
setCustomValidationMessage(),
setDescription(),
setDisabled(),
setError(),
setFieldHolderTemplate(),
setForm(),
setLeftTitle(),
setName(),
setReadonly(),
setRightTitle(),
setSmallFieldHolderTemplate(),
setTabIndex(),
setTemplate(),
setTitle(),
setValue(),
transform(),
validate()
Methods inherited from RequestHandler
allowedActions(),
checkAccessAction(),
getRequest(),
handleRequest(),
hasAction(),
httpError(),
isAjax(),
setDataModel(),
setRequest()
Methods inherited from ViewableData
ATT_val(),
CSSClasses(),
Debug(),
JS_val(),
Me(),
RAW_val(),
SQL_val(),
ThemeDir(),
XML_val(),
__get(),
__isset(),
__set(),
buildCastingCache(),
cachedCall(),
castingClass(),
castingHelper(),
castingHelperPair(),
castingObjectCreator(),
castingObjectCreatorPair(),
customise(),
defineMethods(),
escapeTypeForField(),
getField(),
getIterator(),
getXMLValues(),
hasField(),
hasValue(),
obj(),
renderWith(),
setCustomisedObj(),
setField()
Methods inherited from Object
__call(),
__toString(),
addMethodsFrom(),
addStaticVars(),
addWrapperMethod(),
add_extension(),
add_static_var(),
allMethodNames(),
cacheToFile(),
clearCache(),
combined_static(),
config(),
create(),
createMethod(),
create_from_string(),
exists(),
extend(),
getCustomClass(),
getExtensionInstance(),
getExtensionInstances(),
get_extensions(),
get_extra_config_sources(),
get_static(),
hasExtension(),
hasMethod(),
has_extension(),
invokeWithExtensions(),
is_a(),
loadCache(),
parentClass(),
parse_class_spec(),
remove_extension(),
sanitiseCachename(),
saveCache(),
set_stat(),
set_static(),
set_uninherited(),
stat(),
static_lookup(),
strong_create(),
uninherited(),
uninherited_static(),
useCustomClass()
Magic methods summary
Properties summary
protected
boolean
|
$source |
#
$source Associative or numeric array of all dropdown items, with array key as the submitted field value, and the array value as a natural language description shown in the interface element. |
protected
boolean
|
$isSelected |
#
$isSelected Determines if the field was selected at the time it was rendered,
so if |
protected
boolean
|
$hasEmptyDefault |
#
$hasEmptyDefault Show the first <option> element as empty (not having a
value), with an optional label defined through |
protected
string
|
$emptyString |
#
$emptyString The title shown for an empty default selection, e.g. "Select...". |
Properties inherited from FormField
$attributes,
$containerFieldList,
$customValidationMessage,
$description,
$disabled,
$dontEscape,
$extraClass,
$extraClasses,
$fieldHolderTemplate,
$form,
$leftTitle,
$message,
$messageType,
$name,
$readonly,
$rightTitle,
$smallFieldHolderTemplate,
$template,
$title,
$value
Properties inherited from RequestHandler
$allowed_actions,
$brokenOnConstruct,
$model,
$request,
$url_handlers
Properties inherited from ViewableData
$casting,
$customisedObject,
$default_cast,
$failover
Comments
Use the Silverstripe Forum to ask questions.