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      API reference

Properties
PropertyTypeDefaultDescription
methodstring'post'Form method. Use 'post' or 'get'.
actionstring'./'Form action URL.
protectCSRFbooltrueRender and validate CSRF tokens for POST forms.
prependMarkupstring''Markup inserted immediately after the opening <form> tag.
appendMarkupstring''Markup inserted before the landmark and closing </form> tag.
descriptionstring''Optional form description/headline rendered above child fields.
columnWidthSpacingintautoPixel spacing between column-width children.
confirmTextstring'There are unsaved changes:'Text used with CSS class InputfieldFormConfirm.
Rendering

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 protectCSRF is true.
  • A hidden landmark named _InputfieldForm.

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.

Submission

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:

ArgumentReturn behavior
omitted, '', or falsetrue if the form was submitted, otherwise false.
trueName of the clicked InputfieldSubmit, or false.
stringThat name when its input was submitted; otherwise false.
InputfieldSame 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.

Processing

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 children

Results are cached. Pass null to force a fresh check, available in ProcessWire 3.0.223 and newer.

Form Name

getFormName()

Return the value used in the hidden form landmark. Order of preference:

  1. Form name attribute.
  2. Form id attribute.
  3. Class name InputfieldForm.
$form->attr('name', 'profile_form');
echo $form->getFormName(); // profile_form

In normal rendered forms, an id is usually present or generated, so the fallback is commonly an id such as InputfieldForm1.

Layout Classes

Add these CSS classes to alter form behavior:

ClassEffect
InputfieldFormNoHeightsDo not equalize vertical heights across columns.
InputfieldFormNoWidthsUse a label/input layout where child column widths are ignored.
InputfieldFormConfirmWarn 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 InputfieldFormConfirm.

Hook

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
    }
});
Notes
  • Get a fresh form with $modules->get('InputfieldForm').
  • InputfieldForm is a permanent core module.
  • It extends InputfieldWrapper, so child methods like add(), getChildByName(), getValueByName(), and getErrorInputfields() are inherited.
  • CSRF protection applies to POST forms only.
  • process() is available in ProcessWire 3.0.205 and newer.
  • Source file: wire/modules/Inputfield/InputfieldForm/InputfieldForm.module.

API reference: methods, properties, hooks

Here is an example of creating an InputfieldForm using Inputfield modules. This particular example is an email subscription form.

$form = $modules->get('InputfieldForm');

$f = $form->InputfieldText;
$f->attr('name', 'your_name');
$f->label = 'Your Name';
$form->add($f);

$f = $form->InputfieldEmail;
$f->attr('name', 'your_email');
$f->label = 'Your Email Address';
$f->required = true;
$form->add($f);

$f = $form->InputfieldSubmit;
$f->attr('name', 'submit_subscribe');
$f->val('Subscribe');
$form->add($f);

// ProcessWire versions 3.0.205+
if($form->isSubmitted('submit_subscribe')) {
  if($form->process()) {
    $name = $form->getValueByName('your_name');
    $email = $form->getValueByName('your_email');
    echo "<h3>Thank you, you have been subscribed!</h3>";
  } else {
    echo "<h3>There were errors, please fix</h3>";
    echo $form->render();
  }
} else {
  // form not submitted, just display it
  echo $form->render();
}

// same as above but works in any ProcessWire version
if($input->post('submit_subscribe')) {
  // form submitted
  $form->processInput($input->post);
  $errors = $form->getErrors();
  if(count($errors)) {
    // unsuccessful submit, re-display form
    echo "<h3>There were errors, please fix</h3>";
    echo $form->render();
  } else {
    // successful submit (save $name and $email somewhere)
    $name = $form->getChildByName('your_name')->attr('value');
    $email = $form->getChildByName('your_email')->attr('value');
    echo "<h3>Thank you, you have been subscribed!</h3>";
  }
} else {
  // form not submitted, just display it
  echo $form->render();
}

Optional classes you can add to the InputfieldForm:

  • InputfieldFormNoHeights tells it not to worry about lining up all columns vertically.
  • InputfieldFormNoWidths indicates that form will be in 2-column label => input format (column widths do not apply).
  • InputfieldFormConfirm tell it to notify user if they make any changes and forgot to submit.

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the InputfieldForm class also inherits all the methods and properties of: InputfieldWrapper, Inputfield, WireData and Wire.

Show class?     Show args?       Only hookable?    

Errors

NameReturnSummary 
InputfieldForm::getErrors()
array

Return an array of errors that occurred on any of the children during input processing.

 

Properties

NameReturnSummary 
InputfieldForm::action string Form action attribute
DEFAULT: ./
 
InputfieldForm::appendMarkup string Optional markup to append to the form output
DEFAULT: ''
 
InputfieldForm::columnWidthSpacing int Optionally set the column width spacing (pixels)
DEFAULT: auto
 
InputfieldForm::confirmText string Confirmation text that precedes list of changes when CSS class InputfieldFormConfirm is present
DEFAULT: There are unsaved changes:
 
InputfieldForm::description string Optionally set a description headline for the form
DEFAULT: ''
 
InputfieldForm::method string Form method attribute
DEFAULT: post
 
InputfieldForm::prependMarkup string Optional markup to prepend to the form output
DEFAULT: ''
 
InputfieldForm::protectCSRF bool Set to false to disable automatic CSRF protection
DEFAULT: true
 

Additional methods and properties

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

API reference based on ProcessWire core version 3.0.269