API
Collection of public methods that help you do additional jobs
Usage
After initializing the form with the plugin using $(form).formValidation(options)
, there are two ways to call the plugin method:
or:
The first way mostly returns the FormValidation
instance, meanwhile the second one always returns the jQuery object representing the form.
So, it's possible to chain methods as below:
Below is the list of public methods provided by the plugin.
The methods use the same format as following:
methodName(requiredParameter*, optionalParameter, ...): Type of return value
— The purpose of method
addField
addField(field*, options): FormValidation
— Add a new field.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
options | Object | The field options. If it is not defined, the options are merged by:
|
If you want to do additional task after adding new field, then trigger the added.field.fv event:
Example
defaultSubmit
defaultSubmit(): FormValidation
— Submit the form using default submission.
It also does not perform any validations when submitting the form. It might be used when you want to submit the form right inside the custom submit handler.
destroy
destroy()
— Destroy the plugin.
It will remove all error messages, feedback icons as well as turning off the events created by the plugin.
disableSubmitButtons
disableSubmitButtons(disabled): FormValidation
— Disable or enable the submit buttons
Parameter | Type | Description |
---|---|---|
disabled | Boolean | Can be true or false |
Example
enableFieldValidators
enableFieldValidators(field*, enabled*, validator): FormValidation
— Enable, disable validators to given field
Parameter | Type | Description |
---|---|---|
field | String | The field name |
enabled | Boolean | If true , enable field validators. If false , disable field validators |
validator | String | The validator name. If it is not set, all field validators will be enabled or disabled |
Examples
getDynamicOption
getDynamicOption(field*, option*): String
— Returns the option value which can be set dynamically.
For example, the zipCode validator has country
option that can be changed dynamically a select element.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or element |
option | String | The dynamic option |
getFieldElements
getFieldElements(field): jQuery[]
— Retrieve the field elements by given name.
Returns array of jQuery element representing the field, or null
if the fields are not found.
Parameter | Type | Description |
---|---|---|
field | String | The field name |
getInvalidFields
getInvalidFields(): jQuery[]
— Returns the list of invalid fields.
Example
getMessages
getMessages(field, validator): String[]
— Get the error messages.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element If the field is not defined, the method returns all error messages of all fields |
validator | String | The name of validator If the validator is not defined, the method returns error messages of all validators. |
Example
getOptions
getOptions(field, validator, option): String|Object
— Get the field options.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element If the field is not defined, the method returns the form options. |
validator | String | The name of validator If the validator is not defined, the method returns all field options. |
option | String | The option name If it is not defined, the method returns options of given validator |
getSubmitButton
getSubmitButton(): jQuery
— Returns a jQuery element presenting the clicked submit button. Returns null
if the submit button isn't clicked.
Example
isValid
isValid(): Boolean
— Check the form validity. It can take one of three values:
true
if all fields are validfalse
if there is one invalid fieldnull
if there is at least one field which is not validated yet or being validated
Ensure that the validate() method is already called before calling this one.
isValidContainer
isValidContainer(container*): Boolean
— It can take one of three values:
true
if all fields belonging to the container are validfalse
if all fields belonging to the container are already validated and there is at least one invalid fieldnull
if the container consists of at least one field that isn't validated yet
Using with the validateContainer(container) method, these methods are useful when working with a wizard-like such as tabs, collapsible panels.
Parameter | Type | Description |
---|---|---|
container | String|jQuery | The container selector or container element |
Examples
isValidField
isValidField(field*): Boolean
— Check whether the field is valid or not. It can take one of three values:
true
if the field passes all validatorsfalse
if the field doesn't pass any validatornull
if there is at least one validator which isn't validated yet or being validated
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
Example
removeField
removeField(field*): FormValidation
— Remove given field
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
By triggering the removed.field.fv event, you can perform additional task after removing given field:
Example
resetField
resetField(field*, resetValue): FormValidation
— Reset given field. It hides the error messages and feedback icons.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
resetValue | Boolean | If true , the method resets field value to empty or remove checked/selected attribute (for radio and checkbox). |
Example
resetForm
resetForm(resetFormData): FormValidation
— Reset form. It hides all error elements and feedback icons. All the fields are marked as not validated yet.
Parameter | Type | Description |
---|---|---|
resetFormData | Boolean | If true , the method resets the fields which have validator rules. |
revalidateField
revalidateField(field*): FormValidation
— Revalidate given field.
It's used when you need to revalidate the field which its value is updated by other plugin.
By default, the plugin doesn't re-validate a field once it is already validated and marked as a valid one. When using with other plugins, the field value is changed and therefore need to be re-validated.
Behind the scenes, the method is equivalent to:
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
Example
The following example describes how to re-validate a field which uses with Boostrap Datetime Picker:
updateMessage
updateMessage(field*, validator*, message*): FormValidation
— Update the error message.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
validator | String | The validator name |
message | String | The error message |
Example
updateOption
updateOption(field*, validator*, option*, value*): FormValidation
— Update the option of a specific validator.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
validator | String | The validator name |
option | String | The option name |
value | String | The option value |
Example
updateStatus
updateStatus(field*, status*, validator): FormValidation
— Update validator result for given field
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |
status | String | Can be NOT_VALIDATED , VALIDATING , INVALID or VALID |
validator | String | The validator name. If null , the method updates validity result for all validators |
Example
validate
validate(): FormValidation
— Validate form manually. It is useful when you want to validate form by clicking a button or a link instead of a submit buttons.
validateContainer
validateContainer(container*): FormValidation
— Validate given container.
Using with the isValidContainer(container) method, these methods are useful when working with a wizard-like such as tabs, collapsible panels.
Parameter | Type | Description |
---|---|---|
container | String|jQuery | The container selector or element |
Examples
validateField
validateField(field*): FormValidation
— Validate given field.
Parameter | Type | Description |
---|---|---|
field | String|jQuery | The field name or field element |