FormBuilderForm::addField()

Create and add a new field by specifying name, type and label

You may also specify a FormBuilderField object for the first argument ($name), in which case all following arguments are optional.

Versions prior to 0.5.7 can use the FormBuilderForm::add() method, but this method requires a FormBuilderField object.

Available since version 0.5.7.

Example

$form = $forms->addForm('contact'); 
$form->addField('first_name', 'Text', 'First name');
$form->addField('last_name', 'Text', 'Last name');
$form->addField('email', 'Email', 'Your email');
$form->addField('message', 'Textarea', 'Message');
$form->save();

Usage

// basic usage
$formBuilderField = $formBuilderForm->addField($name);

// usage with all arguments
$formBuilderField = $formBuilderForm->addField($name, string $type = '', string $label = '', array $options = []);

Arguments

NameType(s)Description
$namestring FormBuilderField

Name of field (_a-zA-Z0-9) or a FormBuilderField object.

$type (optional)string

Type of field, which is Inputfield name minus "Inputfield" prefix, i.e. "Text", "URL", "Select", etc. Default:Text

$label (optional)string

Label for this field

$options (optional)array

Additional options

  • description (string): Additional text that appears above input.
  • notes (string): Additional text that appears below input.
  • columnWidth (int): Width of field as a percent between 10 and 100 Default:100
  • required (bool): Specify true to make field required Default:false
  • Additional Inputfield properties unique to each type may be added as well.

Return value

FormBuilderField

Returns the added FormBuilderField object


FormBuilderForm methods and properties

API reference based on ProcessWire core version 3.0.252