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.

Value model

The value is always one of four constants:

ConstantIntegerMeaning
InputfieldToggle::valueYes1Yes / On / True
InputfieldToggle::valueNo0No / Off / False
InputfieldToggle::valueOther2Other (3rd state)
InputfieldToggle::valueUnknown''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.

Properties
PropertyTypeDefaultDescription
labelTypeint0Which label pair to display (see Label types below)
yesLabelstring'✓'Custom yes/on label (used when labelType = labelTypeCustom)
noLabelstring'✗'Custom no/off label (used when labelType = labelTypeCustom)
otherLabelstring'?'Label for the optional third option
useOtherboolfalseShow a third "other" option
useReverseboolfalseReverse the order (No before Yes)
useVerticalboolfalseVertical layout (applies only when inputfieldClass = 'InputfieldRadios')
useDeselectboolfalseAllow clicking the selected option to deselect it (requires defaultOption = 'none')
defaultOptionstring'none'Pre-selected option: 'yes', 'no', 'other', or 'none'
inputfieldClassstring''Delegate rendering to 'InputfieldRadios' or 'InputfieldSelect'; blank = toggle buttons
Label types

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';
Third "other" option
$f->useOther = true;
$f->otherLabel = 'Not sure'; // default: '?'

After processing, check for it with:

if($f->val() === InputfieldToggle::valueOther) { ... }
Rendering

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)
Custom options

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']);
Helper methods
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.
Multi-language

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.

Notes
  • isEmpty() returns true only for valueUnknown (''). valueNo (0) is not empty.
  • Source file: wire/modules/Inputfield/InputfieldToggle/InputfieldToggle.module
API reference: methods, properties, hooks

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 InputfieldToggle class also inherits all the methods and properties of: Inputfield, WireData and Wire.

Show class?     Show args?       Only hookable?    

Common

NameReturnSummary 
InputfieldToggle::addOption($value)
$this

Add a selectable option (custom API usage only, overrides built-in options)

 
InputfieldToggle::formatLabel(string $label)
string

Format label for HTML output (entity encode, etc.)

 
InputfieldToggle::getAllLabels()
array

Get all possible labels for all label types and all languages

 
InputfieldToggle::getConfigAllowContext(Field $field)
array

Return a list of config property names allowed for fieldgroup/template context

InputfieldToggle::getConfigInputfields()
InputfieldWrapper

Configure Inputfield

InputfieldToggle::getInputfield()
mixed

Get the delegated Inputfield that will be used for rendering selectable options

InputfieldToggle::getLabels()
array

Get labels for the given label type

 
InputfieldToggle::getOptions()
array

Get all selectable options as array of [ value => label ]

 
InputfieldToggle::getValueLabel()
string

Get the label for the currently set (or given) value

 
InputfieldToggle::isEmpty()
bool

Is the current value empty? (i.e. no selection)

 
InputfieldToggle::processInput(WireInputData $input)
$this

Process input

InputfieldToggle::render()
string

Render input element(s)

InputfieldToggle::renderReady()
bool

Render ready

 
InputfieldToggle::renderValue()
string

Render value

InputfieldToggle::sanitizeValue($value)
int string

Sanitize the value to be one ofthe constants: valueYes, valueNo, valueOther, valueUnknown

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

Set attribute

 
InputfieldToggle::setOptions(array $options)
$this

Set all options with array of [ value => label ] (custom API usage only, overrides built-in options)

 
InputfieldToggle::wired()
None 

Properties

NameReturnSummary 
InputfieldToggle::defaultOption string Default selected value of 'no', 'yes', 'other' or 'none'
DEFAULT: none
 
InputfieldToggle::inputfieldClass string Inputfield class to use or blank for this toggle buttons
DEFAULT: blank
 
InputfieldToggle::labelType int Label type to use: 0=yes/no, 1=true/false, 2=on/off, 3=enabled/disabled, 100=custom
DEFAULT: 0
 
InputfieldToggle::noLabel string Custom no/off label
DEFAULT:
 
InputfieldToggle::otherLabel string Custom label for optional other value Label to use for "other" option
DEFAULT: ?
 
InputfieldToggle::useDeselect bool int Allow radios or toggles to be de-selected, enabling possibility of no-selection?
DEFAULT: false
 
InputfieldToggle::useOther int bool Use the "other" option?
DEFAULT: false
 
InputfieldToggle::useReverse int bool Reverse the order of the Yes/No options?
DEFAULT: false
 
InputfieldToggle::useVertical bool int Use vertically oriented radio buttons? Applies only if $inputfieldClass is 'InputfieldRadios'
DEFAULT: false
 
InputfieldToggle::value int string Integer value when selection is made or blank string when no selection (0=No, 1=Yes, 2=Other, ''=Unknown) 
InputfieldToggle::yesLabel string Custom yes/on label
DEFAULT:
 

Additional methods and properties

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

API reference based on ProcessWire core version 3.0.267