SilverStripe 3.0 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
Version: master
  • master
  • 3.1
  • 3.0
  • 2.4
  • tagfield

Packages

  • cms
    • assets
    • batchaction
    • batchactions
    • content
    • control
    • controller
    • controllers
    • core
    • export
    • filesystem
    • forms
    • model
    • publishers
    • reports
    • search
    • security
    • tasks
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • framework
    • admin
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • fields-relational
    • filesystem
    • formatters
    • forms
    • gridfield
    • i18n
    • injector
    • integration
    • manifest
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • validation
    • view
  • None
  • PHP

Classes

  • CheckboxField
  • CheckboxField_Readonly
  • CheckboxSetField
  • DropdownField
  • GroupedDropdownField
  • ListboxField
  • LookupField
  • NullableField
  • OptionsetField
  • ReadonlyField
  • TextareaField
  • TextField

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
Extended by ViewableData implements IteratorAggregate
Extended by RequestHandler
Extended by FormField
Extended by DropdownField

Direct known subclasses

CountryDropdownField, GroupedDropdownField, ListboxField, LookupField, OptionsetField

Indirect known subclasses

CheckboxSetField, MemberDatetimeOptionsetField

Package: forms\fields-basic
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.

Creates a new dropdown field.

Parameters

$name
mixed
$name The field name
$title
mixed
$title The field title
$source
mixed
$source An map of the dropdown items
$value
mixed
$value The current value
$form
mixed
$form The parent form
$emptyString
mixed
$emptyString mixed Add an empty selection on to of the DropdownField::$source-Array (can also be boolean, which results in an empty string). Argument is deprecated in 3.1, please use DropdownField::setEmptyString() and/or setHasEmptyDefault(true) instead.

Overrides

FormField::__construct
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.

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.

Parameters

$properties
array
$properties key value pairs of template variables

Returns

string

Overrides

FormField::Field
public array
# getAttributes( )

Returns

array

Overrides

FormField::getAttributes
public boolean
# isSelected( )

Returns

boolean
public array
# getSource( )

Gets the source array including any empty default values.

Gets the source array including any empty default values.

Returns

array
public
# setSource( array $source )

Parameters

$source
array
$source
public
# setHasEmptyDefault( boolean $bool )

Parameters

$bool
boolean
$bool
public boolean
# getHasEmptyDefault( )

Returns

boolean
public
# setEmptyString( string $str )

Set the default selection label, e.g. "select...". Defaults to an empty string. Automatically sets DropdownField::$hasEmptyDefault to true.

Set the default selection label, e.g. "select...". Defaults to an empty string. Automatically sets DropdownField::$hasEmptyDefault to true.

Parameters

$str
string
$str
public string
# getEmptyString( )

Returns

string
public
# performReadonlyTransformation( )

Returns a readonly version of this field

Returns a readonly version of this field

Overrides

FormField::performReadonlyTransformation

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.

$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 FormField::$value matches on of the array values specified in DropdownField::$source

$isSelected Determines if the field was selected at the time it was rendered, so if FormField::$value matches on of the array values specified in DropdownField::$source

protected boolean $hasEmptyDefault
#

$hasEmptyDefault Show the first <option> element as empty (not having a value), with an optional label defined through DropdownField::$emptyString. By default, the <select> element will be rendered with the first option from DropdownField::$source selected.

$hasEmptyDefault Show the first <option> element as empty (not having a value), with an optional label defined through DropdownField::$emptyString. By default, the <select> element will be rendered with the first option from DropdownField::$source selected.

protected string $emptyString
#

$emptyString The title shown for an empty default selection, e.g. "Select...".

$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

Properties inherited from Object

$class, $extension_instances, $extensions

Comments

Comment policy: Please use comments for tips and corrections about the described functionality. Comments are moderated, we reserve the right to remove comments that are inappropriate or are no longer relevant.
Use the Silverstripe Forum to ask questions.
blog comments powered by Disqus
SilverStripe 3.0 API Docs API documentation generated by ApiGen 2.8.0