InputfieldSelect
Single selection <select> dropdown
Base class for all selection-based Inputfields,
including [[Inputfield See [[Inputfield]] for the shared Inputfield API (labels, collapsed states, showIf,
rendering, processing, etc.). Add a single option. If To add a disabled option, pass To pre-select an option, use To add an optgroup, pass the group label as To add a separator (horizontal rule), pass a string of three or more dashes 3.0.236+: Add multiple options at once. By default, treats keys as values and values as labels. When Replace all existing options with the given array. Same signature as Add options from a multi-line string. One option per line. Useful when options come
from a textarea config field or a text block. With separate values and labels, a blank option, and 'Green' pre-selected: Options string format: Optgroups: indent options with 3 or more spaces after a non-indented line: Remove an option by its value. Returns Replace an existing option, preserving its position in the list. Returns Insert new options before an existing option, or prepend to the beginning if
Insert new options after an existing option, or append to the end if Returns all options as an associative array Returns Returns Returns Get or set the label for a given option value (default language). Returns the label string, or Add a translated label for an option value in a specific language 3.0.176+.
If specified, Low-level get/set for language-specific option labels. Accepts Language object, ID,
or name string. Combined get/set for option attributes. Without arguments, returns all option
attributes. With Set (replace) the entire attributes array for an option. Pass an associative array
as Merge attributes into an option's existing attributes without replacing them. Get attributes for a specific option, or all option attributes if When When Returns An Inputfield for handling multiple selection using HTML Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Add options only if they are non-blank Configure Inputfield In addition to the methods and properties above, Inputfield$f = $modules->get('InputfieldProperty Type Default Description sizeint10Number of visible rows in the select box addOption($value, $label = null, $attributes = null)
$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'['disabled' => 'disabled'] as attributes:$f->addOption('tbd', 'To be decided', ['disabled' => 'disabled']);val() after adding options:$f->addOption('green', 'Green');
$f->val('green'); // pre-select green$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']);$f->addOption('---');addOptions(array $options, $assoc = true)
$f->addOptions([
'red' => 'Red',
'green' => 'Green',
'blue' => 'Blue',
]);$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)
addOptions().$f->setOptions(['a' => 'Option A', 'b' => 'Option B']);addOptionsString($value)
$f->addOptionsString("
Red
Green
Blue
");$f->addOptionsString("
=
r=Red
+g=Green
b=Blue
");Syntax Effect LabelValue and label are both Labelvalue=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)Warm colors
r=Red
o=Orange
Cool colors
b=Blue
g=GreenremoveOption($value)
$this.$f->removeOption('red');replaceOption($oldValue, $newValue, $newLabel = null, $newAttributes = null)
true if
found and replaced, false if not found.$f->replaceOption('tbd', 'pending', 'Pending');insertOptionsBefore(array $options, $existingValue = null)
$existingValue is omitted. Returns $this.$f->insertOptionsBefore(['none' => 'None'], 'red');
// inserts 'none' before 'red'insertOptionsAfter(array $options, $existingValue = null)
$existingValue
is omitted. Returns $this.$f->insertOptionsAfter(['other' => 'Other']);
// appends 'other' to the endgetOptions()
[value => label].$options = $f->getOptions();isOption($value)
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)
true if the given option value is currently selected.if($f->isOptionSelected('green')) { ... }isOptionDisabled($value)
true if the given option value has the disabled attribute set.if($f->isOptionDisabled('tbd')) { ... }optionLabel($key, $label = null)
// get label
$label = $f->optionLabel('red'); // 'Red'
// set label
$f->optionLabel('red', 'Dark red');false if the option is not found.addOptionLabel($value, $label, $language = null)
$language can be a Language object, name or ID. $f->addOptionLabel('red', 'Rojo', 'es');optionLanguageLabel($language, $key = null, $label = null)
// 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);optionAttributes($key = null, $attributes = null, $append = false)
$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)
$key to replace ALL option attributes at once.addOptionAttributes($key, array $attrs)
getOptionAttributes($key = null)
$key is omitted.Default value
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 usedvalueAddOption
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 selectedisEmpty()
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
}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.$f->val(['a', 'b']) to set multiple selections.InputfieldHasArrayValue. After processInput(), $f->val() returns an array of selected values.wire/modules/Inputfield/InputfieldConstant Value Description defaultSize10Default number of visible rows <select multiple>.
Also a base type for other multiple selection types (checkboxes, asmSelect, etc.)Inputfield class also inherits all the methods and properties of: InputfieldCommon
Name Return Summary self InputfieldWrapper Properties
Name Return Summary Inputfield int Additional methods and properties
API reference based on ProcessWire core version 3.0.267