Inputfield / InputfieldWrapper
Inputfield is the abstract base class for all form input widgets in ProcessWire
InputfieldWrapper extends it to hold child Inputfields (forms, fieldsets). Together
they are the backbone of ProcessWire's form rendering and input processing.
Inputfields are modules — get a new instance via $modules->get():
$f = $modules->get('InputfieldText');
$f->label = 'Your Name';
$f->attr('name', 'your_name');
$f->attr('value', '');
$f->required = true;Via $modules->get()
$f = $modules->get('InputfieldText'); /** @var InputfieldText $f */
$form = $modules->get('InputfieldForm'); /** @var InputfieldForm $form */
$select = $modules->get('InputfieldSelect'); /** @var InputfieldSelect $select */Each call returns a fresh instance. This is the standard approach when building forms manually.
Via InputfieldWrapper magic property
One benefit of this approach is that it enables IDEs to identify the Inputfield type without a /** @var InputfieldText $f */ type hint.
$f = $wrapper->InputfieldText; // new InputfieldText, not yet added to wrapper
$f = $wrapper->InputfieldSelect;Note: InputfieldForm is a type of InputfieldWrapper.
Via $wrapper->new()
Creates an Inputfield, sets basic properties, adds it to the wrapper, and returns it. Short type names are accepted (without the "Inputfield" prefix).
/** @var InputfieldText $f */
$f = $wrapper->new('InputfieldText', 'phone', 'Phone Number');
$f = $wrapper->new('text', 'email', 'Email', ['required' => true]);
/** @var InputfieldSelect $f */
$f = $wrapper->new('select', 'color', 'Color', ['description' => 'Choose one']);Arguments: $typeName, $name = '', $label = '', $settings = []. Any of $name,
$label, or $settings can be replaced with an array to be treated as $settings.
Attributes
attr($key, $value)
Get or set an HTML attribute. The primary method for working with input attributes.
$f->attr('name', 'email');
$f->attr('value', 'default@example.com');
$name = $f->attr('name'); // get
// Set multiple attributes at once
$f->attr([
'name' => 'email',
'value' => 'default@example.com',
'placeholder' => 'you@example.com',
]);
// Set name and id to the same value
$f->attr('name+id', 'email');
// Get all attributes
$attrs = $f->attr(true);Passing false as the value removes most boolean attributes when setting them. To
remove any attribute explicitly, use $f->removeAttr('attributeName').
val($value)
Shortcut getter/setter for the value attribute.
$current = $f->val(); // get
$f->val('new value'); // setname() / id() / class()
Chainable getter/setter methods:
$f->name('email')->id('input-email')->class('uk-input');
$name = $f->name(); // getLabels and text
| Property | Description |
|---|---|
label | Primary label (appears above input) |
description | Detailed description (appears under label) |
notes | Notes (appears under input) |
detail | Additional detail text (appears under notes, 3.0.140+ |
icon | Font Awesome icon name, without "fa-" prefix |
head | Text between label and description |
tabLabel | Label when rendered as its own tab |
prependMarkup | Markup prepended inside the content container |
appendMarkup | Markup appended inside the content container |
requiredLabel | Custom label for the required-field validation message |
All support magic property set/get (this is the most commonly used syntax):
$f->label = 'Email Address';
$f->description = 'Enter your primary email';
$f->notes = 'We will not share your email';
$f->icon = 'envelope';Collapsed state
Controls how the Inputfield renders in the admin. Set via the collapsed property
using one of the constants below:
$f->collapsed = Inputfield::collapsedYes;
$f->collapsed = Inputfield::collapsedBlankAjax;| Constant | Value | Description |
|---|---|---|
collapsedNo | 0 | Open (default) |
collapsedYes | 1 | Collapsed; user can open |
collapsedBlank | 2 | Collapsed only when blank/empty |
collapsedHidden | 4 | Hidden; not rendered at all |
collapsedPopulated | 5 | Collapsed only when populated |
collapsedNoLocked | 6 | Visible but not editable |
collapsedBlankLocked | 7 | Collapsed when blank; visible-locked when populated |
collapsedYesLocked | 8 | Collapsed; locked when visible |
collapsedLocked | 8 | Alias for collapsedYesLocked |
collapsedNever | 9 | Cannot be collapsed by the user |
collapsedYesAjax | 10 | Collapsed; content loaded via Ajax when opened |
collapsedBlankAjax | 11 | Collapsed when blank; content loaded via Ajax |
collapsedTab | 20 | Rendered as its own tab |
collapsedTabAjax | 21 | Rendered as its own tab; content loaded via Ajax |
collapsedTabLocked | 22 | Rendered as its own tab; locked (not editable) |
Visibility and layout
showIf and requiredIf enable the conditional visibility and required state of
an Inputfield. These are often referred to as Inputfield dependencies. These properties
accept ProcessWire selector strings referencing other field names in the same form.
Note that they support a reduced set of selector features, so test and verify before assuming
a showIf/requiredIf condition works properly. Supported operators include
=, !=, <', <=, >, >=, *=. OR conditions are also supported for field name and value.
// show only when Inputfield `info_type` field has value 'contact'.
$f->showIf = 'info_type=contact';
// required only when condition is met ($f->required must be true)
$f->requiredIf = 'info_type=contact';
$f->required = true;
// percentage width for 2+ Inputfields in a row (10-100; 0 treated as 100)
$f->columnWidth = 50;When matching Page reference fields in a showIf or requiredIf selector, note that you are matching by page ID.
Skip label
Controls label rendering. Use skipLabel constants:
| Constant | Value | Description |
|---|---|---|
skipLabelNo | false | Render label normally (default) |
skipLabelFor | true | Render label without for attribute |
skipLabelHeader | 2 | Don't render the label element at all |
skipLabelBlank | 4 | Skip label if it is blank |
skipLabelMarkup | 8 | Allow HTML markup in the label string |
$f->skipLabel = Inputfield::skipLabelHeader; // no label rendered
$f->skipLabel = Inputfield::skipLabelMarkup; // allow <strong> etc. in labelClass manipulation
$f->addClass('uk-input uk-form-large'); // add to input element
$f->addClass('highlight', 'wrapClass'); // add to .Inputfield wrapper element
$f->addClass('wrap:card, header:card-head'); // formatted element-specific classes
if($f->hasClass('required')) { ... }
$f->removeClass('old-class');The $property argument accepts: 'class', 'wrapClass', 'headerClass', 'contentClass'.
The short names 'input', 'wrap', 'header', and 'content' are also accepted
where class-property names are parsed.
You can also use fluent class methods for common wrapper elements:
$f->wrapClass('card'); // add class to outer .Inputfield wrapper
$f->headerClass('card-head'); // add class to .InputfieldHeader
$f->contentClass('card-body'); // add class to .InputfieldContent
$wrapClass = $f->wrapClass(); // get current wrapper classesProcessing and validation
Common form-handling pattern:
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
/** @var InputfieldEmail $f */
$f = $modules->get('InputfieldEmail');
$f->attr('name', 'email');
$f->label = 'Your email';
$f->required = true;
$form->add($f);
/** @var InputfieldSubmit $f */
$f = $modules->get('InputfieldSubmit');
$f->attr('name', 'submit_send');
$f->value = 'Send';
$form->add($f);
if($input->post('submit_send')) {
$form->processInput($input->post);
if(count($form->getErrors())) {
// re-render form with errors
} else {
/** @var InputfieldEmail $f */
$f = $form->getChildByName('email');
$email = $f->value;
// handle submission
// ...
$session->location('/success/page/url');
}
}
echo $form->render();More modern form handling pattern:
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$f = $form->InputfieldEmail;
$f->attr('name', 'email');
$f->label = 'Your email';
$f->required = true;
$form->add($f);
$f = $form->InputfieldSubmit;
$f->attr('name', 'submit_send');
$f->val('Send');
$form->add($f);
if($form->isSubmitted('submit_send')) {
if($form->process()) {
$email = $form->getValueByName('email');
// handle submission
$session->location('/success/page/url');
} else {
// re-render form with errors
}
}
echo $form->render();process()
Process an InputfieldForm using the form's configured method (post by default,
or get when $form->attr('method', 'get')). Returns true when processing
completed with no errors, or false when validation errors were recorded.
if($form->process()) {
// form processed successfully
} else {
$errors = $form->getErrors();
$errorFields = $form->getErrorInputfields();
}This is often more convenient than processInput() at the form level because it
does not require passing $input->post or $input->get, and it gives you a
success/failure boolean directly.
CSRF protection
InputfieldForm has CSRF protection enabled by default for POST forms.
$form = $modules->get('InputfieldForm');
$form->protectCSRF = true; // defaultWhen the form renders, it automatically includes the CSRF token input. When you
call $form->process() or $form->processInput($input->post), the token is
validated. If the token is invalid, ProcessWire throws a WireException.
Disable CSRF protection only for trusted/internal forms:
$form->protectCSRF = false;CSRF protection is applied to POST forms, not GET forms.
processInput(WireInputData $input)
Process submitted input data for this Inputfield and populate its value.
$f->processInput($input->post);
$value = $f->val(); // now contains the processed valueisEmpty()
Returns true if the Inputfield's current value is considered empty.
error($text) / getErrors($clear) / clearErrors()
$f->error('Value is not valid');
$errors = $f->getErrors(); // array of error strings
$errors = $f->getErrors(true); // get and clear
$f->clearErrors();Rendering
echo $f->render(); // render as an editable input
echo $f->renderValue(); // render as a value display (no input)editable($setEditable)
Get or set whether the Inputfield renders in editable or value-display mode.
$f->editable(false); // lock; renderValue() used instead of render()
$isEditable = $f->editable();Hierarchy
$parent = $f->parent(); // get parent InputfieldWrapper
$parents = $f->getParents(); // all ancestor wrappers
$root = $f->getRootParent(); // top-level wrapper
$form = $f->getForm(); // nearest InputfieldForm ancestorExtends Inputfield to hold an ordered set of child Inputfields. Base class for
forms (InputfieldForm) and fieldsets (InputfieldFieldset).
Adding children
$wrapper->add($f); // append an Inputfield
$wrapper->add('InputfieldText'); // shortcut: create and add by type name
$wrapper->prepend($header);
$wrapper->append($submit);
$wrapper->insertBefore($phone, $email);
$wrapper->insertAfter($address, $phone);Creating and adding in one call
// $wrapper->new() creates, configures, adds, and returns the Inputfield
$f = $wrapper->new('text', 'first_name', 'First Name');
$f->required = true; // chainable further configurationRemoving children
$wrapper->remove($f);
$wrapper->remove('phone'); // remove by name, returns the wrapper
$removed = $wrapper->removeByName('phone'); // remove by name, returns removed field or nullAccessing children
$all = $wrapper->children(); // all children as InputfieldsArray
$required = $wrapper->children('required=1'); // filtered by selector
$f = $wrapper->getChildByName('email'); // single child by name
$f = $wrapper->getByName('email'); // alias
$f = $wrapper->get('email'); // also works (WireData)
$value = $wrapper->getValueByName('email'); // get a child's value directly
$found = $wrapper->find('required=1'); // recursive selector searchBatch import
// Import from an array of definitions
$wrapper->importArray([
[
'type' => 'InputfieldText',
'name' => 'first',
'label' => 'First Name',
'attr' => [
'placeholder' => 'Ada',
],
],
[
'type' => 'InputfieldEmail',
'name' => 'email',
'label' => 'Email',
'required' => true,
],
]);Processing and errors
$wrapper->processInput($input->post); // processes all children
$errors = $wrapper->getErrors(); // errors from all children
$empty = $wrapper->getEmpty(); // required children that are empty
$badFields = $wrapper->getErrorInputfields(); // children with errorsisEmpty() on a wrapper returns true only if ALL children are empty.
Rendering
echo $wrapper->render(); // render all children
echo $wrapper->renderValue(); // render all children in value-display modeMarkup customization
Override the default <ul>/<li> structure globally:
InputfieldWrapper::setMarkup([
'list' => "<div {attrs}>{out}</div>",
'item' => "<div {attrs}>{out}</div>",
]);
InputfieldWrapper::setClasses([
'list' => 'my-form',
'item' => 'my-field {class}',
]);Placeholders: {attrs}, {out}, {class}, {name}, {for}.
These properties are recognized by AdminThemeUikit to control visual presentation:
| Property | Values | Description |
|---|---|---|
themeOffset | 's', 'm', 'l' | Margin/offset size |
themeBorder | 'none', 'card', 'hide', 'line' | Border style |
themeInputSize | 's', 'm', 'l' | Input size |
themeInputWidth | 'xs', 's', 'm', 'l', 'f' | Input width |
themeColor | 'primary', 'secondary', 'warning', etc. | Color theme |
themeBlank | bool | No container/border |
- Source files:
wire/core/Inputfield/Inputfield.phpandInputfieldWrapper.php InputfieldextendsWireDataand implementsModule. All WireData methods are available.$modules->get('InputfieldText')and$form->InputfieldTextreturn a fresh instance each call (not a singleton).showIfandrequiredIfselectors reference other Inputfield names or IDs in the same rendered form.- Each Inputfield
processInput()is called automatically byInputfieldWrapper::processInput()— you usually call it on the form, not individual fields. renderValue()is used in contexts where editing is not allowed (locked fields, view-only displays).collapsedHidden(4) hides the field entirely from render — it won't appear in the DOM at all.- The Tab constants (
collapsedTab,collapsedTabAjax,collapsedTabLocked) render the field as a separate admin tab rather than a collapsible section. - Ajax collapse constants load field content only when the user opens the collapsed section, improving page load on fields with heavy output.
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the InputfieldWrapper class also inherits all the methods and properties of: Inputfield, WireData and Wire.
Common
| Name | Return | Summary | |
|---|---|---|---|
InputfieldWrapper::getValueByName() InputfieldWrapper::getValueByName(string $name) InputfieldWrapper::getValueByName(string $name) | mixed | Get value of Inputfield by name | |
InputfieldWrapper::removeByName() InputfieldWrapper::removeByName(string $name) InputfieldWrapper::removeByName(string $name) | Inputfield null | Remove an Inputfield from the form by name | |
InputfieldWrapper::wired() InputfieldWrapper::wired() InputfieldWrapper::wired() | None | Wired to API |
Manipulation
Retrieval and traversal
Output
| Name | Return | Summary | |
|---|---|---|---|
InputfieldWrapper::render() InputfieldWrapper::render() InputfieldWrapper::render() | string | Render this Inputfield and the output of its children. | |
InputfieldWrapper::renderInputfield() InputfieldWrapper::renderInputfield(Inputfield $inputfield) InputfieldWrapper::renderInputfield(Inputfield $inputfield, bool $renderValueMode = false) | string | Render output for an individual Inputfield | |
InputfieldWrapper::renderValue() InputfieldWrapper::renderValue() InputfieldWrapper::renderValue() | string | Render the output of this Inputfield and its children, showing values only (no inputs) |
Input
| Name | Return | Summary | |
|---|---|---|---|
InputfieldWrapper::processInput() InputfieldWrapper::processInput(WireInputData $input) InputfieldWrapper::processInput(WireInputData $input) | $this | Process input for all children |
For hooks
These methods are only useful for hooking and should not be called directly.
| Name | Return | Summary | |
|---|---|---|---|
InputfieldWrapper::allowProcessInput() InputfieldWrapper::allowProcessInput(Inputfield $inputfield) InputfieldWrapper::allowProcessInput(Inputfield $inputfield) | bool | Allow input to be processed for given Inputfield? (for hooks) |
Errors
| Name | Return | Summary | |
|---|---|---|---|
InputfieldWrapper::getErrorInputfields() InputfieldWrapper::getErrorInputfields() InputfieldWrapper::getErrorInputfields() | array Inputfield | Get Inputfield objects that have errors | |
InputfieldWrapper::getErrors() InputfieldWrapper::getErrors() InputfieldWrapper::getErrors(bool $clear = false) | array | Return an array of errors that occurred on any of the children during input processing. |
Module
| Name | Return | Summary | |
|---|---|---|---|
InputfieldWrapper::getConfigInputfields() InputfieldWrapper::getConfigInputfields() InputfieldWrapper::getConfigInputfields() | InputfieldWrapper | Get configuration Inputfields for this InputfieldWrapper |
Properties
Access any common Inputfield type class name from an InputfieldWrapper and it will return a new instance of that Inputfield, i.e. $f = $inputfields->InputfieldText; Below are several examples.
| Name | Return | Summary | |
|---|---|---|---|
| InputfieldWrapper::InputfieldAsmSelect | InputfieldAsmSelect | Create new asmSelect Inputfield | |
| InputfieldWrapper::InputfieldButton | InputfieldButton | Create new button Inputfield | |
| InputfieldWrapper::InputfieldCKEditor | InputfieldCKEditor | Create new CKEditor Inputfield | |
| InputfieldWrapper::InputfieldCheckbox | InputfieldCheckbox | Create new checkbox Inputfield | |
| InputfieldWrapper::InputfieldCheckboxes | InputfieldCheckboxes | Create new checkboxes Inputfield | |
| InputfieldWrapper::InputfieldDatetime | InputfieldDatetime | Create new date/time Inputfield | |
| InputfieldWrapper::InputfieldEmail | InputfieldEmail | Create new email Inputfield | |
| InputfieldWrapper::InputfieldFieldset | InputfieldFieldset | Create new Fieldset InputfieldWrapper | |
| InputfieldWrapper::InputfieldFile | InputfieldFile | Create new file Inputfield | |
| InputfieldWrapper::InputfieldFloat | InputfieldFloat | Create new float Inputfield | |
| InputfieldWrapper::InputfieldForm | InputfieldForm | Create new form InputfieldWrapper | |
| InputfieldWrapper::InputfieldHidden | InputfieldHidden | Create new hidden Inputfield | |
| InputfieldWrapper::InputfieldIcon | InputfieldIcon | Create new icon Inputfield | |
| InputfieldWrapper::InputfieldImage | InputfieldImage | Create new image Inputfield | |
| InputfieldWrapper::InputfieldInteger | InputfieldInteger | Create new integer Inputfield | |
| InputfieldWrapper::InputfieldMarkup | InputfieldMarkup | Create new markup Inputfield | |
| InputfieldWrapper::InputfieldPage | InputfieldPage | Create new Page selection Inputfield | |
| InputfieldWrapper::InputfieldPageAutocomplete | InputfieldPageAutocomplete | Create new Page selection autocomplete Inputfield | |
| InputfieldWrapper::InputfieldPageListSelect | InputfieldPageListSelect | Create new PageListSelect Inputfield | |
| InputfieldWrapper::InputfieldPageListSelectMultiple | InputfieldPageListSelectMultiple | Create new multiple PageListSelect Inputfield | |
| InputfieldWrapper::InputfieldRadios | InputfieldRadios | Create new radio buttons Inputfield | |
| InputfieldWrapper::InputfieldSelect | InputfieldSelect | Create new <select> Inputfield | |
| InputfieldWrapper::InputfieldSelectMultiple | InputfieldSelectMultiple | Create new <select multiple> Inputfield | |
| InputfieldWrapper::InputfieldSubmit | InputfieldSubmit | Create new submit button Inputfield | |
| InputfieldWrapper::InputfieldText | InputfieldText | Create new single-line text Inputfield | |
| InputfieldWrapper::InputfieldTextTags | InputfieldTextTags | Create new text tags Inputfield | |
| InputfieldWrapper::InputfieldTextarea | InputfieldTextarea | Create new multi-line <textarea> Inputfield | |
| InputfieldWrapper::InputfieldToggle | InputfieldToggle | Create new toggle Inputfield | |
| InputfieldWrapper::InputfieldURL | InputfieldURL | Create new URL Inputfield | |
| InputfieldWrapper::InputfieldWrapper | InputfieldWrapper | Create new generic InputfieldWrapper |
Additional methods and properties
In addition to the methods and properties above, InputfieldWrapper also inherits the methods and properties of these classes:
API reference based on ProcessWire core version 3.0.266