Stop performing remaining validators if there is a validator that the field does not pass
By default, when a field is being validated, all validators for a field will be executed. Let's take a look at a simple signing up form that its username field has to pass all of the following validator rules:
After each key stroke, all validators will perform their jobs. Some of them might be expensive tasks such as checking the validity in the server side via the remote validator.
It is better if the expensive validator is performed at the end when the field passes all other remaining validators. It reduces the number of server side requests (for example, it will not hit the database server to check if an username is taken). Also, our users don't have to wait for these validators to run and return the results. It brings a better user experience.
The Sequence plugin is handy for this requirement. It will stop performing a given validator if the field does not pass the previous validator.
The following piece of code is the starting point to use the Sequence plugin:
The sample code above assumes that the FormValidation files are placed inside the
vendors directory. You might need to change the path depending on where you place them on the server.
| ||Boolean or Object||It can be |
In the following form, all validators are performed in sequential order. A validator will not be executed if the field doesn't pass the previous validator.
By putting the remote validator at the end, we only send a request to server to check if the username is taken if it passes all other validators.
For testing purpose, the demo always answers that the username is already taken no matter what you input.
- v1.1.0: First release. It means that the Sequence plugin requires FormValidation v1.1.0 or newer.