A Field object represents a form field of a particular type. Each field has its own unique attributes and functionality. Whenever you're dealing with a field in your template, you're actually working with a Field object.
Returns the JavaScript modules for the front-end field.
Phone
Setting
Description
countryAllowed
A collection of allowed countries.
countryDefaultValue
The default value for the Country sub-field.
countryEnabled
Whether the Country sub-field is enabled in the control panel.
Products
Setting
Description
placeholder
The option shown initially, when no option is selected.
sources
Which sources do you want to select products from?
limit
Limit the number of selectable products.
Radio
Setting
Description
options
Define the available options for users to select from.
layout
Select which layout to use for these fields. Either vertical or horizontal,
Recipients
Setting
Description
displayType
What sort of field to show on the front-end for users. Either hidden, dropdown, checkboxes or radio.
options
Define the available options for users to select from.
Repeater
Setting
Description
addLabel
The label for the button that adds another instance.
minRows
The minimum required number of instances of this repeater's fields that must be completed.
maxRows
The maximum required number of instances of this repeater's fields that must be completed.
Section
Setting
Description
border
Add a border to this section.
borderWidth
Set the border width (in pixels).
borderColor
Set the border color.
Signature
Setting
Description
backgroundColor
Set the background color.
penColor
Set the pen color.
penWeight
Set the line thickness (weight) for the pen.
Single-Line Text
Setting
Description
placeholder
The text that will be shown if the field doesn’t have a value.
defaultValue
Entering a default value will place the value in the field when it loads.
limit
Whether to limit the content of this field.
minType
The field’s minimum text type. Either characters or words.
min
The field’s minimum number of characters/words to limit, based on minType.
maxType
The field’s maximum text type. Either characters or words.
max
The field’s maximum number of characters/words, based on maxType.
Table
Setting
Description
columns
Define the columns your table should have.
defaults
Define the default values for the field.
addRowLabel
The label for the button that adds another row.
static
Whether this field should disallow adding more rows, showing only the default rows.
minRows
The minimum required number of rows in this table that must be completed.
maxRows
The maximum required number of rows in this table that must be completed.
Tags
Setting
Description
placeholder
The option shown initially, when no option is selected.
source
Which source do you want to select tags from?
Users
Setting
Description
placeholder
The option shown initially, when no option is selected.
sources
Which sources do you want to select users from?
limit
Limit the number of selectable users.
Variants
Setting
Description
placeholder
The option shown initially, when no option is selected.
source
Which source do you want to select variants from?
limit
Limit the number of selectable variants.
Custom Validation
You can add custom validation to field, to handle all manner of scenarios. To do this, you'll need to create a custom module to contain PHP code for the validation logic.
If you write your own plugin or module, you’ll want to use its init() method to subscribe to an event on the Submission object to add your validation rules. Your event listener can add additional validation rules(opens new window) for fields.
For example, let's say we have a field with a handle emailAddress which we'd like required. We could do this with the following.
use verbb\formie\elements\Submission;
use verbb\formie\events\SubmissionRulesEvent;
use yii\base\Event;
Event::on(Submission::class, Submission::EVENT_DEFINE_RULES, function(SubmissionRulesEvent $event) {
$event->rules[] = [['field:emailAddress'], 'required'];
});
Or, maybe we only want a field required when another field is a certain value.
You can also customise the validation message with number validation when using min/max with tooSmall and tooBig.
However, the above rules are applied to every submission, which will throw an error if you set a rule for a field that doesn't exist on the form for the submission you're creating. The above example assumes you have a field with the handle emailAddress for every form, which may not always be the case, especially if you have multiple forms.
Instead, you'll want to add a conditional check what form you're creating a submission on.
Event::on(Submission::class, Submission::EVENT_DEFINE_RULES, function(SubmissionRulesEvent $event) {
$form = $event->submission->getForm();
// Only apply this custom validation for the form with handle `contactForm`
if ($form->handle === 'contactForm') {
$event->rules[] = [['field:emailAddress'], 'required'];
}
});
If you have a lot of forms, or would rather not conditionally check every form for your site, you can loop through the available fields for the submission, to add a check whether the field exists. This can be useful if you want to enforce a validation for all email fields across your site, but not every form has an emailAddress field.
Event::on(Submission::class, Submission::EVENT_DEFINE_RULES, function(SubmissionRulesEvent $event) {
foreach ($event->submission->getFields() as $field) {
// Check against the handle of the field
if ($field->handle === 'emailAddress') {
$event->rules[] = [['field:emailAddress'], 'required'];
}
// Or, for a more global-check - against the type of the field
if ($field instanceof \verbb\formie\fields\Email) {
$event->rules[] = [['field:emailAddress'], 'required'];
}
}
});