rules
prop allows validation of form components. rules
is an object where each prop is a name of global validator or user defined function with tested value as argument. User functions should return true
for valid result or the string
message if the result is invalid. Global validators include:
required
alpha
numeric
alphanumeric
minLength
maxLength
minElements
maxElements
minValue
maxValue
email
atLeastOneUppercase
atLeastOneLowercase
atLeastOneDigit
atLeastOneSpecial
sameAs
Default messages of global validators can be changed by setValidationMessage(validator, message)
function. If validator has argument (for example maxLength
) %d
token can be used in message in place of that argument.
import { setValidationMessage } from "vue-components"
setValidationMessage("maxLength", "Please enter up to %d characters")
model: ""
rules: [ "required", { "minLength": 5 }, "alphanumeric" ]
status: { "required": false, "minLength": false, "alphanumeric": false, "valid": false, "optional": false, "touched": false, "validated": false, "dirty": false }
model: ""
rules: [ "required", "atLeastOneUppercase", "atLeastOneLowercase", "atLeastOneSpecial", "atLeastOneDigit", { "minLength": 8 } ]
status: { "required": false, "atLeastOneUppercase": false, "atLeastOneLowercase": false, "atLeastOneSpecial": false, "atLeastOneDigit": false, "minLength": false, "valid": false, "optional": false, "touched": false, "validated": false, "dirty": false }
model: []
rules: [ "required", { "minElements": 3 }, { "maxElements": 5 } ]
status: { "required": false, "minElements": false, "maxElements": true, "valid": false, "optional": false, "touched": false, "validated": false, "dirty": false }
validation-on
and validation-mode
allows control over when to start validation and how to update state according to validation results:
validate-on: "blur"
- validate after input loses focus (default
) validate-on: "immediate"
- starts validating immediately after first input validate-on: "form"
- validate after calling validate
function of form component validate-mode: "silent"
- valid values does not change input state unless it was invalid before (only for validate on blur) (default
) validate-mode: "eager"
- invalid and valid values always change input state model: ""
rules: [
"required",
{
"minLength": 5
},
"alphanumeric"
]
status: {
"required": false,
"minLength": false,
"alphanumeric": false,
"valid": false,
"optional": false,
"touched": false,
"validated": false,
"dirty": false
}
model: ""
rules: [
"required",
{
"minLength": 5
},
"alphanumeric"
]
status: {
"required": false,
"minLength": false,
"alphanumeric": false,
"valid": false,
"optional": false,
"touched": false,
"validated": false,
"dirty": false
}
model: ""
rules: [
"required",
{
"minLength": 5
},
"alphanumeric"
]
status: {
"required": false,
"minLength": false,
"alphanumeric": false,
"valid": false,
"optional": false,
"touched": false,
"validated": false,
"dirty": false
}
In order to validate entire form wrap inputs with v-form
component which exposes two functions: validate()
and reset()
. Calling validate()
function validates every input according to its rules and return true
if all are valid (false
if any input is invalid). To reset all inputs within v-form
use exposed reset()
function.
username: { "valid": false, "validated": false }
password: { "valid": false, "validated": false }
text: { "valid": false, "validated": false }
form validate(): false