InputfieldForm
Top-level container for building and processing ProcessWire forms
It extends
InputfieldWrapper and adds form-specific behavior: <form> rendering,
automatic CSRF token rendering/validation, submission detection, and high-level
processing helpers.
$form = $modules->get('InputfieldForm');
$form->attr('id', 'contact-form');
$f = $form->InputfieldText;
$f->attr('name', 'name');
$f->label = 'Your name';
$f->required = true;
$form->add($f);
$f = $form->InputfieldSubmit;
$f->attr('name', 'submit_contact');
$f->val('Send');
$form->add($f);
if($form->isSubmitted('submit_contact')) {
if($form->process()) {
$name = $form->getValueByName('name');
// handle successful submission
} else {
echo $form->render();
}
} else {
echo $form->render();
} For child management, wrapper rendering, showIf, requiredIf, column widths,
and inherited Inputfield behavior, see InputfieldWrapper and Inputfield.
Expand all Collapse all API reference
| Property | Type | Default | Description |
|---|---|---|---|
method | string | 'post' | Form method. Use 'post' or 'get'. |
action | string | './' | Form action URL. |
protectCSRF | bool | true | Render and validate CSRF tokens for POST forms. |
prependMarkup | string | '' | Markup inserted immediately after the opening <form> tag. |
appendMarkup | string | '' | Markup inserted before the landmark and closing </form> tag. |
description | string | '' | Optional form description/headline rendered above child fields. |
columnWidthSpacing | int | auto | Pixel spacing between column-width children. |
confirmText | string | 'There are unsaved changes:' | Text used with CSS class Inputfield. |
render()
Render the complete <form> element, including child inputfields, optional
description, prependMarkup, appendMarkup, CSRF token, and the form landmark.
echo $form->render();For POST forms, render() outputs:
- A CSRF token when
protectCSRFis true. - A hidden landmark named
_Inputfield.Form
The landmark lets isSubmitted() distinguish this form from other forms on the
same request.
For GET forms, CSRF token and landmark markup are omitted.
If the current request includes modal=1, the form action is adjusted to retain
that modal state.
isSubmitted($submitName = '')
Return whether the current request submitted this form. Build the full form before calling this method so it can inspect submit buttons and child names.
if($form->isSubmitted()) {
// this form was submitted
}
if($form->isSubmitted('submit_save')) {
// submitted by the save button
}
$button = $form->isSubmitted(true);
if($button === 'submit_save') {
// save button clicked
}Argument behavior:
| Argument | Return behavior |
|---|---|
omitted, '', or false | true if the form was submitted, otherwise false. |
true | Name of the clicked InputfieldSubmit, or false. |
| string | That name when its input was submitted; otherwise false. |
Inputfield | Same as string, using the inputfield's name. |
For POST forms, isSubmitted() checks the request method, the form landmark, and
the CSRF token when protectCSRF is true. CSRF failure returns false here;
process() / processInput() throw on invalid CSRF tokens.
process()
Process the form using the configured request method and return true when no
errors were found.
if($form->process()) {
$email = $form->getValueByName('email');
} else {
$errors = $form->getErrors();
echo $form->render();
}process() calls processInput() with $input->post or $input->get depending
on method.
processInput(WireInputData $input)
Lower-level processing method for supplied input data.
$form->processInput($input->post);
if(!count($form->getErrors())) {
// success
}For POST forms with protectCSRF enabled, invalid CSRF tokens throw a
WireException. Disable CSRF only for trusted/internal forms:
$form->protectCSRF = false;processInput() also resolves showIf and requiredIf dependencies so delayed
children are processed in dependency order.
getInput()
Return the WireInputData passed to the most recent processInput() call, or
null before processing.
$inputData = $form->getInput();getErrors($clear = false)
Return child inputfield errors.
$errors = $form->getErrors();
$errors = $form->getErrors(true); // get and clear
$errors = $form->getErrors(null); // clear cache and re-check childrenResults are cached. Pass null to force a fresh check, available in
ProcessWire 3.0.223 and newer.
getFormName()
Return the value used in the hidden form landmark. Order of preference:
- Form
nameattribute. - Form
idattribute. - Class name
Inputfield.Form
$form->attr('name', 'profile_form');
echo $form->getFormName(); // profile_formIn normal rendered forms, an id is usually present or generated, so the fallback
is commonly an id such as Inputfield.
Add these CSS classes to alter form behavior:
| Class | Effect |
|---|---|
Inputfield | Do not equalize vertical heights across columns. |
Inputfield | Use a label/input layout where child column widths are ignored. |
Inputfield | Warn about unsaved changes when leaving the form. |
$form->addClass('InputfieldFormNoHeights');
$form->addClass('InputfieldFormConfirm');
$form->confirmText = 'You have unsaved changes.'; If the FormSaveReminder module is installed, it controls unsaved-change
behavior instead of Inputfield.
renderOrProcessReady($type)
Hook called before rendering or processing. $type is 'render' or 'process'.
$wire->addHookBefore('InputfieldForm::renderOrProcessReady', function(HookEvent $event) {
$form = $event->object;
$type = $event->arguments(0);
if($type === 'process') {
// inspect or adjust the form before processing
}
}); - Get a fresh form with
$modules->get('Inputfield.Form') Inputfieldis a permanent core module.Form - It extends InputfieldWrapper, so child methods like
add(),getChildByName(),getValueByName(), andgetErrorInputfields()are inherited. - CSRF protection applies to POST forms only.
process()is available in ProcessWire 3.0.205 and newer.- Source file:
wire/modules/Inputfield/Inputfield.Form/Inputfield Form.module
API reference: methods, properties, hooks
Here is an example of creating an Inputfield Optional classes you can add to the Inputfield Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Get name for this form Return WireInputData provided to processInput() method or null if not yet applicable Is form submitted and ready to process? Process the form Process input Render form Return an array of errors that occurred on any of the children during input processing. In addition to the methods and properties above, Inputfield$form = $modules->get('InputfieldInputfield tells it not to worry about lining up all columns vertically.Inputfield indicates that form will be in 2-column label => input format (column widths do not apply).Inputfield tell it to notify user if they make any changes and forgot to submit.Inputfield class also inherits all the methods and properties of: InputfieldWrapper, Inputfield, WireData and Wire.Common
Name Return Summary string WireInputData null bool string boolInputfieldWrapperstringErrors
Name Return Summary array Properties
Name Return Summary Inputfield string Form action attribute
DEFAULT: ./ Inputfield string Optional markup to append to the form output
DEFAULT: '' Inputfield int Optionally set the column width spacing (pixels)
DEFAULT: auto Inputfield string Confirmation text that precedes list of changes when CSS class Inputfield
DEFAULT: There are unsaved changes: Inputfield string Optionally set a description headline for the form
DEFAULT: '' Inputfield string Form method attribute
DEFAULT: post Inputfield string Optional markup to prepend to the form output
DEFAULT: '' Inputfield bool Set to false to disable automatic CSRF protection
DEFAULT: true Additional methods and properties
API reference based on ProcessWire core version 3.0.269