InputfieldToggle
On/off toggle with a configurable label pair (Yes/No, On/Off, True/False, etc
) and an optional third "other" state. Renders as toggle buttons by default; can delegate to [[InputfieldRadios]] or [[InputfieldSelect]].
$f = $modules->get('InputfieldToggle');
$f->name = 'active';
$f->label = 'Active?';
$f->val(InputfieldToggle::valueYes);
$form->add($f); See [[Inputfield]] for the shared Inputfield API.
The value is always one of four constants:
| Constant | Integer | Meaning |
|---|---|---|
Inputfield | 1 | Yes / On / True |
Inputfield | 0 | No / Off / False |
Inputfield | 2 | Other (3rd state) |
Inputfield | '' | No selection |
isEmpty() returns true only when the value is valueUnknown (empty string). The
integer 0 (valueNo) is a present, non-empty value.
sanitizeValue() accepts flexible input and normalises it to one of the four
constants above: booleans, integers 0/1/2, strings 'yes'/'no'/'on'/
'off'/'true'/'false', or any label matching the current labelType.
| Property | Type | Default | Description |
|---|---|---|---|
labelType | int | 0 | Which label pair to display (see Label types below) |
yesLabel | string | '✓' | Custom yes/on label (used when labelType = labelTypeCustom) |
noLabel | string | '✗' | Custom no/off label (used when labelType = labelTypeCustom) |
otherLabel | string | '?' | Label for the optional third option |
useOther | bool | false | Show a third "other" option |
useReverse | bool | false | Reverse the order (No before Yes) |
useVertical | bool | false | Vertical layout (applies only when inputfieldClass = 'InputfieldRadios') |
useDeselect | bool | false | Allow clicking the selected option to deselect it (requires defaultOption = 'none') |
defaultOption | string | 'none' | Pre-selected option: 'yes', 'no', 'other', or 'none' |
inputfieldClass | string | '' | Delegate rendering to 'InputfieldRadios' or 'InputfieldSelect'; blank = toggle buttons |
Five built-in label pairs are available via constants:
$f->labelType = InputfieldToggle::labelTypeYes; // Yes / No (default)
$f->labelType = InputfieldToggle::labelTypeTrue; // True / False
$f->labelType = InputfieldToggle::labelTypeOn; // On / Off
$f->labelType = InputfieldToggle::labelTypeEnabled; // Enabled / Disabled
$f->labelType = InputfieldToggle::labelTypeCustom; // use yesLabel / noLabel For custom labels:
$f->labelType = InputfieldToggle::labelTypeCustom;
$f->yesLabel = 'Yes please';
$f->noLabel = 'No thanks'; Icon names from the admin icon set may be embedded in custom labels:
$f->yesLabel = 'icon-check Yes';
$f->noLabel = 'icon-times No';$f->useOther = true;
$f->otherLabel = 'Not sure'; // default: '?'After processing, check for it with:
if($f->val() === InputfieldToggle::valueOther) { ... } By default, toggle buttons are rendered. To use radios or a select instead:
$f->inputfieldClass = 'InputfieldRadios'; // render as radio buttons
$f->inputfieldClass = 'InputfieldSelect'; // render as a <select>
$f->useVertical = true; // vertical layout (radios only)addOption() and setOptions() replace the built-in Yes/No/Other model entirely
with an arbitrary set of options. Values must be integers in the range -128–127.
Not available when used with FieldtypeToggle.
$f->addOption(1, 'Approved');
$f->addOption(2, 'Pending');
$f->addOption(0, 'Rejected');
// or all at once:
$f->setOptions([1 => 'Approved', 2 => 'Pending', 0 => 'Rejected']);getValueLabel($value = null, $labelType = null, $language = null)- Returns the display label for a given value (or the current value if omitted).
getLabels($labelType = null, $language = null)- Returns an array with keys
'yes','no','other','unknown'for the given label type and language. getOptions()- Returns
[value => label]array of currently configured options.
yesLabel, noLabel, and otherLabel support per-language overrides when
ProcessWire's LanguageSupport module is active. Language variants are set as
yesLabel{languageId}, noLabel{languageId}, etc.
isEmpty()returnstrueonly forvalueUnknown('').valueNo(0) is not empty.- Source file:
wire/modules/Inputfield/InputfieldToggle/Inputfield Toggle.module
An Inputfield for handling an on/off toggle that maintains a boolean value (or null when no selection). This provides an alternative to a single checkbox field.
API usage example
// Default behavior displays toggle of "Yes" and "No":
$f = $modules->get('InputfieldToggle');
$f->attr('name', 'test_toggle_field');
$f->label = 'Do you like toggle fields?';
// Optionally make it show "On" and "Off" (rather than "Yes" and "No"):
$f->labelType = InputfieldToggle::labelTypeOn;
// Optionally set custom labels:
$f->labelType = InputfieldToggle::labelTypeCustom;
$f->yesLabel = 'Yes please';
$f->noLabel = 'No thanks';
// Optionally add an "other" option with label "Not sure":
$f->useOther = true;
$f->otherLabel = 'Not sure';
// Set the value: 0=No, 1=Yes, 2=Other, or blank string '' for no selection (Unknown)
$f->val(1);
// Optionally set to use radio buttons rather than toggle buttons:
$f->inputfieldClass = 'InputfieldRadios';
// Remember to add to your InputfieldForm, InputfieldWrapper or InputfieldFieldset:
$form->add($f);
echo $form->render(); Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Inputfield class also inherits all the methods and properties of: Inputfield, WireData and Wire.
Common
Properties
| Name | Return | Summary | |
|---|---|---|---|
| Inputfield | string | Default selected value of 'no', 'yes', 'other' or 'none' DEFAULT: none | |
| Inputfield | string | Inputfield class to use or blank for this toggle buttons DEFAULT: blank | |
| Inputfield | int | Label type to use: 0=yes/no, 1=true/false, 2=on/off, 3=enabled/disabled, 100=custom DEFAULT: 0 | |
| Inputfield | string | Custom no/off label DEFAULT: ✗ | |
| Inputfield | string | Custom label for optional other value Label to use for "other" option DEFAULT: ? | |
| Inputfield | bool int | Allow radios or toggles to be de-selected, enabling possibility of no-selection? DEFAULT: false | |
| Inputfield | int bool | Use the "other" option? DEFAULT: false | |
| Inputfield | int bool | Reverse the order of the Yes/No options? DEFAULT: false | |
| Inputfield | bool int | Use vertically oriented radio buttons? Applies only if $inputfieldClass is 'InputfieldRadios' DEFAULT: false | |
| Inputfield | int string | Integer value when selection is made or blank string when no selection (0=No, 1=Yes, 2=Other, ''=Unknown) | |
| Inputfield | string | Custom yes/on label DEFAULT: ✓ |
Additional methods and properties
In addition to the methods and properties above, Inputfield
API reference based on ProcessWire core version 3.0.267