Vue validation library with directive and data based approch.
npm install vue-nice-validate
npm install vue-nice-validate
`
If you want to use it with Vue2, then you can use
`
npm install vue-nice-validate@2
`
Usage
$3
The VueNiceValidate provide Vue3 composable function useVueNiceValidate
you have to run this function to get tools(Array and functions) to perform validation in your page.
For basic validation you need to import and use at least 4 entities,
vValidate for using directive
validateForm or validateInputs or validateInput to check if perticular input fields are valid w.r.t data.
formErrors for showing errors in template
Import and use useVueNiceValidate
`js
import {useVueNiceValidate} from 'vue-nice-validate';
const {vValidate, formErrors, validateForm, validationFields} = useVueNiceValidate();
`
Use as directive
You can add validation rules via v-validate directive for template usage
`js
const { vValidate } = useVueNiceValidate();
//options api
export default {
...
directives: {
"validate": vValidate,
},
...
}
`
`html
`
The html element must contain id attribute. This id attribute must contain unique value which should reperesnt variable(reacive property) name. This package take rules from your directive in template and values from your reactive properties, And combine them with help of id attribute.
so for let email = ref('') you can use
for let loginForm = reactive({email:''}) you can use
for let userProfile = reactive({emails:[{email:''}]}) you can use
use as Object
You can also add validation rules via function addValidationFields which accepts the following syntax
`js
const { addValidationFields } = useVueNiceValidate();
const validationFields = {
'email': {
'rules': 'required|email',
'field_name': $t('email'),
'form_name': '', //optional
'validate_all_rules': false, //optional
}
}
addValidationFields(validationFields);
`
Check validation
`js
const { validateForm } = useVueNiceValidate();
...
async submit(){
let is_form_valid = await validateForm(loginForm);
if(!is_form_valid){
//form is invalid
return false;
}
//form is valid
//call api
}
...
`
validateForm is a promise which resolves with boolean result, it can be used just before calling api,
Or in 'onMounted' if you want to start validationg form and show errors on pageLoad
This functions will add respective field errors to formErrors reactive array.
show Form Errors
$3
`js
const { formErrors } = useVueNiceValidate();
//options api
export default {
...
data() {
return {
...
formErrors: formErrors,
...
}
},
...
}
`
$3
`html
{{ formErrors['field_id'] }}
`
$3
If you are using internationalization or wants your custom validation messages to show instead of default ones,
Then you can use messageFormatter option in ValidatePlugin
`js
import {ValidatePlugin} from 'vue-nice-validate';
...
const messageFormatter = (rule, params)=>{
return i18n.global.t(rule.toUpperCase(), params)
};
app.use(ValidatePlugin,{messageFormatter});
...
`
$3
To validate on selected input fields you can pass object containing only those fields.
`js
const { validateForm } = useVueNiceValidate();
...
async submit(){
//composition API
let is_form_valid = await validateForm( ()=>({email,password}) );
//optional API
let is_form_valid = await validateForm( ()=>({email:this.email}) );
if(!is_form_valid){
//form is invalid
return false;
}
//form is valid
//call api
}
...
`
$3
To validate single form if you are using multiple forms in component,
You need to pass form name as parameter in validateForm and formWatcher
You need to mention form name in template and with field ids as well
`html
`
`js
const { validateForm } = useVueNiceValidate();
...
let loginForm = reactive({email,password});
async login(){
if(!await validateForm(loginForm, 'loginForm')){
//loginForm is invalid
return false;
}
...
}
let registerForm = reactive({email,password});
async register(){
if(!await validateForm(registerForm, 'registerForm')){
//registerForm is invalid
return false;
}
...
}
...
`
$3
As formErrors is available as reactive property, you can play with it in case you want to add server error or custom errors.
`js
const { formErrors } = useVueNiceValidate();
...
formErrors['email'] = 'This email is already registered. Please Login.';
...
``