InputfieldText
Single-line text input
The most commonly used Inputfield — renders an <input type="text">
element and provides server-side validation for length, pattern, and content.
$f = $modules->get('InputfieldText');
$f->name = 'first_name';
$f->label = 'First name';
$f->placeholder = 'Enter your first name';
$f->required = true;
$f->minlength = 2;
$f->maxlength = 20; For the shared Inputfield API (attributes, labels, collapsed states, showIf, rendering, processing, etc.), see [[Inputfield]].
| Property | Type | Default | Description |
|---|---|---|---|
type | string | 'text' | HTML input type attribute |
size | int | 0 | Display width in characters; 0 = full width |
minlength | int | 0 | Minimum character length (0 = no minimum) |
maxlength | int | 2048 | Maximum character length; 0 = no limit (see note) |
placeholder | string | '' | Placeholder text shown when input is empty |
pattern | string | '' | HTML5 regex pattern for client/server validation |
initValue | string | '' | Initial value shown when no value is set |
stripTags | bool | false | Strip HTML tags from the value when set |
noTrim | bool | false | Prevent whitespace trimming of the value |
useLanguages | bool | false | Provide one input per language (requires LanguageSupport) |
requiredAttr | bool\|int | false | When combined with required, adds HTML5 required attribute |
showCount | int | 0 | Show character counter (1) or word counter (2) or none (0) |
autocomplete | string\|null | null | HTML5 autocomplete attribute 3.0.252 |
| Constant | Value | Description |
|---|---|---|
defaultMaxlength | 2048 | Default maxlength when none specified |
showCountNone | 0 | No counter |
showCountChars | 1 | Character counter |
showCountWords | 2 | Word counter |
Inputfield When Note: minlength is only enforced when a value is present. If the field is not
required and left blank, no minlength error is triggered. When When When By default, leading and trailing whitespace is trimmed from the value. Set
processInput():Minimum length
minlength is set to a positive value and the input is not empty, the value
must meet the minimum character length. If it doesn't, an error is recorded.$f->minlength = 5;
// Error if input has fewer than 5 charactersMaximum length
maxlength is set to a positive value, input exceeding it is truncated and
an error message is recorded informing the user. When maxlength is 0, there is
no limit. The default is 2048.$f->maxlength = 100;
// Input over 100 chars is truncated, error shownPattern
pattern is set to a valid regex, the value is validated against it both
client-side (via HTML5 pattern attribute) and server-side. On the server side,
the pattern is applied as a PCRE regex with # delimiters (escaped automatically).$f->pattern = '^[a-zA-Z0-9_]+$';
// Only alphanumeric and underscore allowedTag stripping
stripTags is true, HTML tags are removed from the value via PHP's
strip_tags() during value assignment.$f->stripTags = true;
$f->val('<b>Hello</b>'); // stores 'Hello'Whitespace trimming
noTrim to true to preserve whitespace.$f->noTrim = true;
$f->val(' hello '); // stores ' hello '
When showCount is set to showCountChars (1) or showCountWords (2), a
JavaScript counter is displayed below the input showing the current character or
word count. When combined with minlength or maxlength, the counter also
displays the minimum/maximum limits.
$f->showCount = InputfieldText::showCountChars;
$f->maxlength = 140;
// Shows "X characters (140 max)" The initValue property provides a default value that populates the input's value
attribute when no actual value has been set. If the user doesn't change it, the
initValue will be submitted with the form just like any other value.
$f->initValue = 'Enter your name';
// If $f->val() is empty, the input's value attribute becomes "Enter your name"The autocomplete property sets the HTML5 autocomplete attribute. Available
options depend on the input type:
- For
type='text':any HTML5 autocomplete token (e.g.'username','given-name','email','tel') - For
type='email':'on','off', or'email' - For
type='tel':'on','off', or'tel'
$f->autocomplete = 'username';When useLanguages is true and the LanguageSupport module is installed, the
Inputfield renders one input per language. Each language's value is accessed via
value[languageID], and placeholders can also be set per language via
placeholder[languageID].
// enables multi-language support for Inputfield types
$f->useLanguages = true;
// get a non-default language
// $language can be Language (Page) object, ID or name (i.e. 'es')
$language = $languages->get("es");
// set 'value' for language
$f->setLanguageValue($language, "Spanish value");
$f->setLanguageValue("es", "Spanish value"); // same as above
// this also works if $language is not 'default' language
$f->set("value$language", "Spanish value");
// get 'value' for non-default language
$value = $f->getLanguageValue($language);
$value = $f->getLanguageValue("es"); // same as above
// this also works if $language is not 'default 'language'
$value = $f->get("value$language");
// get or set 'default' language value using val()
$f->val("Default language value");
$value = $f->val();
// set label, description or notes
$f->set("label", "Your name"); // default language label
$f->set("label$language", "Tu nombre"); // label for other language(s)
$f->label = __('Your name'); // or make it admin translatable
// setting other properties
$f->set("placeholder", "Enter your name"); // default language
$f->set("placeholder$language", "Escribe tu nombre"); // other language(s)
$f->set("placeholder", __('Enter your name')); // admin translatableWhen used as a Field on a Page (via FieldtypeText), the Inputfield's settings are configured on the Field object and applied automatically. See [[FieldtypeText]] for details on how text fields store and format values.
For standalone forms (module config, Process modules, front-end forms), create and configure the Inputfield directly as shown in the examples above.
- The default
maxlengthof 2048 is suitable for most single-line text inputs. Set to 0 for no limit. - Values are sanitized via
$sanitizer->text()whenmaxlengthis positive, enforcing max byte length as well as character length. - The
sizeattribute only affects the visual width of the input; it does not limit the number of characters that can be entered (usemaxlengthfor that). - When
sizeis 0 (default), the input uses full width with theInputfieldMaxWidthCSS class. - The
minlengthattribute is converted todata-minlengthin the rendered HTML because browser support for the nativeminlengthattribute is limited. - Source file:
wire/modules/Inputfield/Inputfield.Text/Inputfield Text.module
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 null | HTML5 autocomplete attribute, used only by text and email, not other descending types 3.0.252+ DEFAULT: null | |
| Inputfield | string | Optional initial/default value DEFAULT: blank | |
| Inputfield | int | Maximum allowed length of value or 0 for no min DEFAULT: 0 | |
| Inputfield | int | Minimum allowed length of value or 0 for no max (usually combined with 'required' option) DEFAULT: 0 | |
| Inputfield | bool | By default whitespace is trimmed from value, set this to true to bypass that behavior . DEFAULT: false | |
| Inputfield | string | HTML5 pattern attribute for front-end validation DEFAULT: blank | |
| Inputfield | string | Placeholder attribute text DEFAULT: blank | |
| Inputfield | bool int | When combined with "required" option, this also makes it use the HTML5 "required" attribute. DEFAULT: false | |
| Inputfield | int | Show a character counter (1) or word counter (2) or neither (0). Recommended value is 1 when using minlength or maxlength. DEFAULT: 0 | |
| Inputfield | int | Size of input or 0 for full width DEFAULT: 0 | |
| Inputfield | bool | Should HTML tags be stripped from value? DEFAULT: false | |
| Inputfield | string | Input type DEFAULT: text | |
| Inputfield | bool | When combined with multi-language support, setting this to true will provide one input per language. Get/set each language value with the "value[languageID]" property, and just "value" for default language. DEFAULT: false |
Additional methods and properties
In addition to the methods and properties above, Inputfield
API reference based on ProcessWire core version 3.0.267