InputfieldSelect

Single selection <select> dropdown

Base class for all selection-based Inputfields, including [[InputfieldSelectMultiple]], [[InputfieldCheckboxes]], [[InputfieldRadios]], and [[InputfieldAsmSelect]]. All option management methods documented here are available in those subclasses as well.

$f = $modules->get('InputfieldSelect');
$f->name = 'color';
$f->label = 'Favorite color';
$f->addOption('red', 'Red');
$f->addOption('green', 'Green');
$f->addOption('blue', 'Blue');
$f->val('green'); // pre-select green
$form->add($f);

See [[Inputfield]] for the shared Inputfield API (labels, collapsed states, showIf, rendering, processing, etc.).

Properties
PropertyTypeDefaultDescription
sizeint10Number of visible rows in the select box
Adding options

addOption($value, $label = null, $attributes = null)

Add a single option. If $label is omitted, the value is used as the label. Returns $this.

$f->addOption('red', 'Red');
$f->addOption('none'); // value and label are both 'none'

To add a disabled option, pass ['disabled' => 'disabled'] as attributes:

$f->addOption('tbd', 'To be decided', ['disabled' => 'disabled']);

To pre-select an option, use val() after adding options:

$f->addOption('green', 'Green');
$f->val('green'); // pre-select green

To add an optgroup, pass the group label as $value and an array of [value => label] as $label:

$f->addOption('Warm colors', ['red' => 'Red', 'orange' => 'Orange', 'yellow' => 'Yellow']);
$f->addOption('Cool colors', ['blue' => 'Blue', 'green' => 'Green']);

To add a separator (horizontal rule), pass a string of three or more dashes 3.0.236+:

$f->addOption('---');

addOptions(array $options, $assoc = true)

Add multiple options at once. By default, treats keys as values and values as labels.

$f->addOptions([
 'red' => 'Red',
 'green' => 'Green',
 'blue' => 'Blue',
]);

When $assoc is false, the array values are used for both value and label (keys ignored):

$f->addOptions(['Red', 'Green', 'Blue'], false);
// adds options where value and label are both 'Red', 'Green', 'Blue'

setOptions(array $options, $assoc = true)

Replace all existing options with the given array. Same signature as addOptions().

$f->setOptions(['a' => 'Option A', 'b' => 'Option B']);

addOptionsString($value)

Add options from a multi-line string. One option per line. Useful when options come from a textarea config field or a text block.

$f->addOptionsString("
Red
Green
Blue
");

With separate values and labels, a blank option, and 'Green' pre-selected:

$f->addOptionsString("
=
r=Red
+g=Green
b=Blue
");

Options string format:

SyntaxEffect
LabelValue and label are both Label
value=LabelSeparate value and label
=Blank option (empty value, empty label)
+LabelPre-selected option
++LabelLiteral +Label (escaped leading plus)
disabled:LabelDisabled option
---Separator / horizontal rule 3.0.236+
== in value or labelLiteral = (escaped equals sign)

Optgroups: indent options with 3 or more spaces after a non-indented line:

Warm colors
 r=Red
 o=Orange
Cool colors
 b=Blue
 g=Green

removeOption($value)

Remove an option by its value. Returns $this.

$f->removeOption('red');

replaceOption($oldValue, $newValue, $newLabel = null, $newAttributes = null)

Replace an existing option, preserving its position in the list. Returns true if found and replaced, false if not found.

$f->replaceOption('tbd', 'pending', 'Pending');

insertOptionsBefore(array $options, $existingValue = null)

Insert new options before an existing option, or prepend to the beginning if $existingValue is omitted. Returns $this.

$f->insertOptionsBefore(['none' => 'None'], 'red');
// inserts 'none' before 'red'

insertOptionsAfter(array $options, $existingValue = null)

Insert new options after an existing option, or append to the end if $existingValue is omitted. Returns $this.

$f->insertOptionsAfter(['other' => 'Other']);
// appends 'other' to the end

getOptions()

Returns all options as an associative array [value => label].

$options = $f->getOptions();
Querying options

isOption($value)

Returns true if the given value is a valid option (including within optgroups). Separator values are never considered valid options.

if($f->isOption('red')) {
 // 'red' is available
}

isOptionSelected($value)

Returns true if the given option value is currently selected.

if($f->isOptionSelected('green')) { ... }

isOptionDisabled($value)

Returns true if the given option value has the disabled attribute set.

if($f->isOptionDisabled('tbd')) { ... }
Option labels

optionLabel($key, $label = null)

Get or set the label for a given option value (default language).

// get label
$label = $f->optionLabel('red'); // 'Red'
// set label
$f->optionLabel('red', 'Dark red');

Returns the label string, or false if the option is not found.

addOptionLabel($value, $label, $language = null)

Add a translated label for an option value in a specific language 3.0.176+. If specified, $language can be a Language object, name or ID.

$f->addOptionLabel('red', 'Rojo', 'es');

optionLanguageLabel($language, $key = null, $label = null)

Low-level get/set for language-specific option labels. Accepts Language object, ID, or name string.

// set one label
$f->optionLanguageLabel('es', 'red', 'Rojo');
// set multiple labels for a language at once
$f->optionLanguageLabel('es', ['red' => 'Rojo', 'green' => 'Verde']);
// get all labels for a language
$labels = $f->optionLanguageLabel('es');
// remove all labels for a language
$f->optionLanguageLabel('es', false);
Option attributes

optionAttributes($key = null, $attributes = null, $append = false)

Combined get/set for option attributes. Without arguments, returns all option attributes. With $key only, returns attributes for that option. With $attributes array, sets (or appends) attributes for that option.

// get all option attributes
$all = $f->optionAttributes();
// get attributes for one option
$attrs = $f->optionAttributes('red'); // e.g. ['disabled' => 'disabled']
// set attributes for one option
$f->optionAttributes('red', ['class' => 'highlight']);
// append attributes (merge, don't replace)
$f->optionAttributes('red', ['data-hex' => '#ff0000'], true);

setOptionAttributes($key, array $attrs)

Set (replace) the entire attributes array for an option. Pass an associative array as $key to replace ALL option attributes at once.

addOptionAttributes($key, array $attrs)

Merge attributes into an option's existing attributes without replacing them.

getOptionAttributes($key = null)

Get attributes for a specific option, or all option attributes if $key is omitted.

Value handling

Default value

When defaultValue is set and the field is required and currently empty, the default value is applied automatically during render() and after processInput().

$f->required = true;
$f->defaultValue = 'green';
// if no value is selected, 'green' is used

valueAddOption

When valueAddOption is true, setting a value via the API that is not already a defined option automatically adds it as an option. Applies only to values set from code — not to user-submitted input (security).

$f->valueAddOption = true;
$f->val('purple'); // 'purple' is added as an option and selected

isEmpty()

Returns true when no option is selected. The value '0' is considered empty only when '0' is not a defined option.

if($f->isEmpty()) {
 // nothing selected
}
Security

processInput() validates all submitted values against the defined options list and silently removes any that are not present. This prevents injection of arbitrary values through manipulated form submissions.

Notes
  • Value is always an array. Use $f->val(['a', 'b']) to set multiple selections.
  • Blank options are silently ignored — multi-select has no concept of a "blank" selection.
  • Implements InputfieldHasArrayValue. After processInput(), $f->val() returns an array of selected values.
  • Source file: wire/modules/Inputfield/InputfieldSelectMultiple.module
Constants
ConstantValueDescription
defaultSize10Default number of visible rows
API reference: methods, properties, hooks

Serves as the base for Inputfields that provide selection of options (whether single or multi). As a result, this class includes functionality for, and checks for both single-and-multi selection values. Subcalsses will want to override the render method, but it's not necessary to override processInput(). Subclasses that select multiple values should implement the InputfieldHasArrayValue interface.


Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the InputfieldSelect class also inherits all the methods and properties of: Inputfield, WireData and Wire.

Show class? Show args? Only hookable?

Common

NameReturnSummary
InputfieldSelect::addOption(string $value)
$this

Add an option that may be selected

InputfieldSelect::addOptionAttributes($key)
$this

Add attributes for an item (without removing existing attributes), or for multiple items

InputfieldSelect::addOptionLabel($value, string $label)
$this

Add selectable option with label, optionally for specific language

InputfieldSelect::addOptions(array $options)
$this

Add multiple options at once

InputfieldSelect::addOptionsString(string $value)
$this

Given a multi-line string, convert it to options, one per line

InputfieldSelect::get(string $key)
array mixed null

Get property

InputfieldSelect::getConfigInputfields()
InputfieldWrapper

Field configuration

InputfieldSelect::getOptionAttributes()
array

Get an attributes array intended for an item (or for all items)

InputfieldSelect::getOptionAttributesString($key)
string

Get an attributes string intended for the <option> element

InputfieldSelect::getOptions()
array

Get all options for this Select

InputfieldSelect::insertOptionsAfter(array $options)
self

Insert new options after an existing option

InputfieldSelect::insertOptionsBefore(array $options)
self

Insert new options before an existing option (or prepend options to beginning)

InputfieldSelect::isEmpty()
bool

Is the value empty?

InputfieldSelect::isOption($value)
bool

Returns whether the provided value is one of the available options

InputfieldSelect::isOptionDisabled($value)
bool

Is the given option value disabled?

InputfieldSelect::isOptionSelected($value)
bool

Returns whether the provided value is selected

InputfieldSelect::optionAttributes()
array

Get or set option attributes


Can also be used as property: InputfieldSelect::optionAttributes
InputfieldSelect::optionLabel($key)
string bool

Get or set label for given option value/key (default language)

InputfieldSelect::optionLanguageLabel($language)
string array Inputfield

Get or set alternative language label(s)

InputfieldSelect::processInput(WireInputData $input)
$this

Process input from the provided array

InputfieldSelect::removeOption($value)
$this

Remove the option with the given value

InputfieldSelect::render()
string

Render and return the output for this Select

InputfieldSelect::renderOptions()
string

Render the given options

InputfieldSelect::renderReady()
bool

Render ready

InputfieldSelect::renderValue()
string

Render non-editable value

InputfieldSelect::replaceOption($oldValue, $newValue)
bool

Replace an option already present with the new value (and optionally new label and attributes)

InputfieldSelect::set(string $key, mixed $value)
Inputfield InputfieldSelect

Set property

InputfieldSelect::setAttribute($key, $value)
Inputfield InputfieldSelect

Set attribute

InputfieldSelect::setOptionAttributes($key)
$this

Set/replace entire attributes array for an item

InputfieldSelect::setOptions(array $options)
$this

Set/replace all options

Properties

NameReturnSummary
InputfieldSelect::defaultValue string int
InputfieldSelect::options array string Get or set options, array of [value => label], or use options string.
InputfieldSelect::valueAddOption bool If value attr set from API (only) that is not an option, add it as an option? 3.0.171+
DEFAULT: false

Additional methods and properties

In addition to the methods and properties above, InputfieldSelect also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.267

AltStyle によって変換されたページ (->オリジナル) /