Settings
Settings structure
There are three levels of settings which are form, field, and validators.
The FormValidation plugin can be called as following:
Most of settings can be set via HTML 5 attributes prefixed with data-fv
.
For example, the following call:
is equivalent with the one below:
In the next sections, you will see the full list of options for each level.
Form settings
Below is the list of settings for form sorted in alphabetical order:
Option | Equivalent HTML attribute | Default |
---|---|---|
autoFocus |
data-fv-autofocus |
true |
button |
|
|
err |
|
|
excluded |
data-fv-excluded |
[':disabled', ':hidden', ':not(:visible)'] |
framework |
data-fv-framework |
bootstrap |
icon |
|
|
live |
data-fv-live |
'enabled' |
message |
data-fv-message |
'This value is not valid' |
row |
|
|
threshold |
data-fv-threshold |
null |
trigger |
data-fv-trigger |
null |
verbose |
data-fv-verbose |
true |
fields |
n/a | null |
autoFocus
autoFocus: Boolean
— Indicate the first invalid field will be
focused on automatically. It is true
, by default.
You also can set this option for particular field.
button
selector
selector: String
— The CSS selector indicates the submit buttons.
Clicking on these buttons will validate the form. They will be disabled when the form input are invalid to prevent the valid form from multiple submissions.
disabled
disabled: String
— The CSS class for disabled button. The default
value is defined by specific frameworks as following:
Framework | Disabled button class |
---|---|
Bootstrap | disabled |
Foundation | disabled |
Pure | pure-button-disabled |
Semantic | disabled |
UIKit | disabled |
Example
err
clazz
clazz: String
— The CSS class of message element. The default
value is taken from specific framework if available:
Framework | err.clazz default value |
---|---|
Bootstrap 3 | help-block |
Bootstrap 4 | text-help |
Foundation 5 | error |
Foundation 6 | form-error |
Pure | fv-help-block |
Semantic | ui red pointing label |
UIKit | uk-text-danger |
container
container: String|Function
— Indicate where the error messages are
shown. It is null
by default.
Value | Description |
---|---|
CSS selector | All error messages are placed in element defined by this CSS selector |
A callback |
A callback returns the element that the messages are placed in: |
tooltip |
Error messages of each field are placed inside a tooltip |
popover |
Error messages of each field are placed inside a popup |
The tooltip or popover is provided by specific framework:
Framework | Tooltip | Popover |
---|---|---|
Bootstrap | Bootstrap's tooltip | Bootstrap's popover |
Foundation | Foundation's tooltip | |
Pure | n/a | |
Semantic | Semantic's popup | |
UIKit | UIKit's tooltip |
This option can be overridden by particular field.
ejemplos
The following form uses Bootstrap tooltip to show the error message.
excluded
excluded: String|String[]|Function
— Indicate fields which won't
be validated.
By default, the plugin will not validate the following kind of fields:
- disabled
- hidden
- invisible
The setting consists of jQuery filters. Accept 3 formats:
Format | Description |
---|---|
String |
Filters are separated by a comma. For example: |
Array of strings |
Each element is a filter. For example: |
Array of strings and callback functions |
The callback function has format as below: For example: |
It is also possible to excluded particular field.
The excluded
option is usually used when we need to validate the field
generated by other UI plugin. For an usage, you can take a look at compatibility ejemplos.
framework
framework: String
— Indicate the framework you are using.
It can be one of following values:
Value | Framework | Latest supported version |
---|---|---|
bootstrap default |
Bootstrap 3 | 3.3.7 |
bootstrap4 |
Bootstrap 4 | 4.0.0 alpha 3 |
foundation5 |
Foundation 5 | 5.5.3 |
foundation |
Foundation 6 | 6.2.3 |
pure |
Pure | 0.6.0 |
semantic |
Semantic UI | 2.2.2 |
uikit |
UIKit | 2.26.4 |
icon
Indicate valid/invalid/validating icons based on the field validity.
By default, these icons are not set. You also can enabled or disable feedback icon for particular field.
- When using Bootstrap framework, ensure that you are using Bootstrap v3.1.0 or later
- When using Semantic framework, wrap the field inside
You can use icons provided by
Icon sets | Example |
---|---|
Glyphicons | |
FontAwesome |
There is a FontAwesome issue (#41313) that causes the icon to be aligned not properly when using with Bootstrap. While waiting for this issue to be fixed, you can resolve it by placing the FontAwesome CSS before Bootstrap CSS: |
Semantic icons | |
UIKit |
UIKit uses the FontAwesome icons,
but replaces the |
Using with Bootswatch
Some BootsWatch themes override some CSS styles
causing feedback icon invisible to your eyes. For instance, the Flatly theme set the feedback icon
color to #FFF
.
To fix this, you can simply add some CSS to your head
, right before the
BootsWatch theme CSS, to reset the feedback icons color:
Example
live
live: String
— Live validating mode. Can be one of three values:
Value | Description |
---|---|
enabled default |
The plugin validates fields as soon as they are changed |
disabled |
Disable the live validating. The error messages are only shown after the form is submitted |
submitted |
The live validating is enabled after the form is submitted |
There is no live
option for particular field. If you don't want the
field to be validated as soon as you change its value, you can use the trigger and threshold options.
message
message: String
— The default error message for all fields. You
can specify the error message for any fields or validators.
row
selector
selector: String
— CSS selector indicates the parent element of
field. Each framework usually wraps both field and its label inside elements with
the same CSS class
The default value of this option is defined by specific framework:
Framework | row.selector default value |
---|---|
Bootstrap | .form-group |
Foundation | .row |
Pure | .pure-control-group |
Semantic | fields |
UIKit | .uk-form-row |
This option might be used in the following cases:
- The form uses a custom CSS class for each group
- Each group consist of more than one field
You also can specify the row for particular field.
Example
In the following form, each field is placed inside a cell of table. The error messages are placed inside a tooltip.
valid
valid: String
— The CSS class for success row that contains valid
field
invalid
invalid: String
— The CSS class for error row that contains
invalid field
The default values of valid
and invalid
options are defined
as following:
Framework | row.valid default value | row.invalid default value |
---|---|---|
Bootstrap | has-success |
has-error |
Foundation | fv-has-success |
error |
Pure | fv-has-success |
fv-has-error |
Semantic | fv-has-success |
error |
UIKit | fv-has-success |
fv-has-error |
Adding custom class to field container while it is being validated
There's no built-in option like row.validating
but it's possible to add
a custom class to the field container while validating it.
To archive it, you can trigger the status.field.fv event. The following code snippet demonstrates how to do it while the field is being validated by the remote validator:
Example
threshold
threshold: Number
— The field will not be live validated if its
length is less than this number of characters. You also can set this option for particular field.
trigger
trigger: String
— The event which is fired to validating all
fields when the live validating mode is enabled. If you need multiple events are
fired, then separate them by a space.
It's also possible to set trigger option for each field. Look at the field trigger section.
verbose
verbose: Boolean
— Whether to be verbose when validating a field
or not.
Value | Description |
---|---|
true | When a field has multiple validators, all of them will be checked respectively. If errors occur in multiple validators, all of them will be displayed to the user |
false | when a field has multiple validators, validation for this field will be terminated upon the first encountered error. Thus, only the very first error message related to this field will be displayed to the user |
It's also possible to set verbose option for each field. Look at the field verbose section.
Field settings
Below is the list of Field settings:
Option | Equivalent HTML attribute |
---|---|
autoFocus |
data-fv-autofocus |
enabled |
data-fv-enabled |
err |
data-fv-err |
excluded |
data-fv-excluded |
icon |
data-fv-icon |
message |
data-fv-message |
row |
data-fv-row |
selector |
data-fv-selector |
threshold |
data-fv-threshold |
trigger |
data-fv-trigger |
verbose |
data-fv-verbose |
autoFocus
autoFocus: Boolean
— Indicate the field will be focused on
automatically if it is not valid. It is true
, by default.
In some case, you don't want the invalid field to be focused automatically. For example, focusing on a field using a date picker might show up the picker, which seems to be annoyed.
enabled
enabled: Boolean
— Enable or disable the field validators.
If you want to enable or disable particular validator, use validator's enabled option or enableFieldValidators() method.
Example
err
err: String|Function
— Indicate where the error messages are
shown.
Value | Description |
---|---|
CSS selector | Error messages are placed in element defined by this CSS selector |
A callback |
A callback returns the element that the messages are placed in: |
tooltip |
Error messages are placed inside a tooltip. |
popover |
Error messages are placed inside a popover. |
Example
The following form illustrates an usage of the err
option. The error
messages are shown in the element defined by a CSS selector.
excluded
excluded: Boolean|Function
— Indicate whether or not the field is
excluded.
To exclude a set of fields, you should use the excluded option for form.
Example
icon
icon: Boolean
— Enable or disable feedback icons.
Example
The form below disables the feedback icons for field by setting
icon: false
or icon: 'false'
message
message: String
— The default error message for the field.
row
row: String
— CSS selector indicates the parent element of field
You can set the same selector for all fields in form by applying the row.selector option for form.
selector
selector: String
— The CSS selector to indicate the field. It is
used in case that it's not possible to use the name
attribute for the
field.
Example
Instead of using the name
attribute, the following form uses the
selector
option to define the fields:
As you see, the field can be defined by a ID (#ccNumber
), class
(.cvvNumber
) or attribute ([data-stripe="exp-month"]
)
selector.
threshold
threshold: Number
— Do not live validate field until the length of
field value exceed this number.
Example
trigger
trigger: String
— The field events (separated by a space) which
are fired when the live validating mode is enabled.
For example, trigger="focus blur"
means that the field will be validated
when user focus on or leave the focus off the field.
Example
In the following form, the Title field will be validated while user
type any character (trigger="keyup"
). The Summary
field will be validated when user lose the focus (trigger="blur"
).
verbose
verbose: Boolean
— Whether to be verbose when validating a field
or not.
Look at the form verbose section to see the possible value for the verbose option.
Validator settings
The following table shows the common settings for validators (when using it, replace
{validatorname}
with the validator name). For specific settings of each
validator, please look at its documentation.
Option | Equivalent HTML attribute |
---|---|
enabled |
data-fv-{validatorname} |
message |
data-fv-{validatorname}-message |
transformer |
n/a |
enabled
enabled: Boolean
— Indicate the validator is enabled or disabled.
It is true
, by default.
Look at the field's enabled option if you want to enable/disable all validators.
The table below shows three equivalent ways to enable or disable given validator:
Usage | Example |
---|---|
HTML 5 attribute | |
Plugin option | |
enableFieldValidators() |
ejemplos
message
message: String
— The error message of validator for field.
transformer
transformer: Function
— Modify the field value before validating.
Example
The following form accepts a website address without http:// or https:// prefix.
By default, these kind of URLs don't pass the uri
validators. Applying the transformer
option for the uri validator, we
can make it pass.
Dynamic option
Some validators have option which its value could be change dynamically.
For example, the zipCode validator has the
country
option that can be changed dynamically a select element.
The dynamic option can be determined by:
- A string
- Name of field which defines the value
- Name of function which returns the value
- A function returns the value
In the third and fourth cases, the callback function must follow the format:
Supported validators
Below is the list of validators supporting dynamic option. Refer to specific validator document to see the full list of options.
Validator | Dynamic option |
---|---|
between validator | min , max |
choice validator | min , max |
date validator | min , max |
greaterThan validator | value |
iban validator | country |
id validator | country |
lessThan validator | value |
phone validator | country |
stringLength validator | min , max |
vat validator | country |
zipCode validator | country |
To illustrate how powerful this concept is, take a look at the following example.
Assume that your form uses zipCode validator to validate a zipcode. The next sections show you how to use four ways above to define option value.
You can use the following sample zipcodes for testing:
Country | Valid Zipcode | Invalid Zipcode |
---|---|---|
United States | 12345 | 123 |
Italy | IT-12345 | 123 |
Using string as usual
It's easy for you if the country code option is set initially and can't be changed:
What happen if you want the country to be changeable, for example, by a select
element. With the dynamic option concept, it can be done easily by setting the
country
option as:
Using name of element defining the option value
Using name of function returning the option value
Using function returning the option value
Dynamic message
Looking back to the zipcode example above, you will realize that the message is static:
As you see, the message is always The value is not valid zipcode
and
does not change nomatter what the country is.
Fortunately, it is easy to convert this message to a dynamic one. Just use
%s
characters in the message and they will be replaced with the country
you choose:
Below is the list of validators supporting dynamic message:
Validator | Example |
---|---|
between validator | |
choice validator | |
greaterThan validator | |
iban validator | |
id validator | |
lessThan validator | |
phone validator | |
stringLength validator | |
vat validator | |
zipCode validator | |
callback validator | |
promise validator | |
remote validator |
The backend returns a JSON string that consists of
|
Your own validator |
Turning off dynamic message
To turn off dynamic message:
- Don't use
%s
characters in the message - Don't include the
message
in the result returned by callback, promise, remote validator, or your own validator.
Validator | Example |
---|---|
between validator | |
callback validator | |
promise validator | |
remote validator |
The backend returns a JSON string that consists of
|
Your own validator |
Events
Similar to the settings, there are also events for form, field and validator. Each event can be set by one of three ways:
- Listening to event using jQuery
on(eventName, callback)
- Using option
- Using HTML 5 attributes
Form events
Event | Description |
---|---|
init.form.fv |
Triggered after the form is initialized by the plugin |
prevalidate.form.fv |
Triggered before validating the form |
err.form.fv |
Triggered when the form is invalid |
success.form.fv |
Triggered when the form is valid |
added.field.fv |
Triggered after adding dynamic field |
removed.field.fv |
Triggered after removing given field |
Usage | Example |
---|---|
Listening to event | |
Using option | |
Using HTML 5 attributes |
ejemplos
Field events
Event | Description |
---|---|
init.field.fv |
Triggered after the field is initialized by the plugin |
err.field.fv |
Triggered when any field is invalid |
success.field.fv |
Triggered when any field is valid |
status.field.fv |
Triggered when field changes status. Each field has four possible status:
|
Usage | Example |
---|---|
Listening to event |
If you want to trigger this event for particular field, for example, the
name="email"
field:
Usage | Example |
---|---|
Listening to event | |
Using option | |
Using HTML 5 attributes |
ejemplos
- Clearing field when clicking the icon
- Enabling the submit button all the time
- Getting notified while field is being validated
- Hiding success class
- Showing icons in custom area
- Showing messages in custom area
- Showing valid message
- Using Google material icons
- Using hint library to show message
- Validating checkbox list placed in multiple columns
- Validating fields that depend on each other
Validator events
Event | Description |
---|---|
err.validator.fv |
Triggered when field doesn't pass given validator |
success.validator.fv |
Triggered when field passes given validator |
Usage | Example |
---|---|
Listening to event | |
Using option | |
Using HTML 5 attributes |