Skip to content

Commit b2fedf1

Browse files
toadkickermosinve
authored andcommitted
Added vee-validate example (#1889)
* Added vee-validate example Added example for vee-validate and fixed example for vuelidate state property * validateState method that was missing
1 parent bb1c550 commit b2fedf1

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

docs/markdown/reference/validation/README.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This is a verbose example designed to show how Bootstrap-Vue and Vuelidate inter
2121
<b-form-input id="exampleInput1"
2222
type="text"
2323
v-model="form.name"
24-
:state="!$v.form.name.$invalid"
24+
:state="$v.form.name.$dirty ? !$v.name.$error : null"
2525
aria-describedby="input1LiveFeedback"
2626
placeholder="Enter name" />
2727
<b-form-invalid-feedback id="input1LiveFeedback">
@@ -33,7 +33,7 @@ This is a verbose example designed to show how Bootstrap-Vue and Vuelidate inter
3333
label-for="exampleInput2">
3434
<b-form-select id="exampleInput2"
3535
:options="foods"
36-
:state="!$v.form.food.$invalid"
36+
:state="$v.form.food.$dirty ? !$v.name.$error : null"
3737
v-model="form.food" />
3838
<b-form-invalid-feedback id="input2LiveFeedback">
3939
This is a required field
@@ -86,3 +86,77 @@ This is a verbose example designed to show how Bootstrap-Vue and Vuelidate inter
8686

8787
<!-- form-validation-1.vue -->
8888
```
89+
90+
## vee-validate
91+
92+
[vee-validate](https://github.com/baianat/vee-validate) is a plugin for Vue.js that allows you to validate input fields and display errors. It has full support for `vue-i18n` and provides fairly good out of the box error messages.
93+
94+
Same example as above just modified for vee-validate:
95+
96+
```html
97+
<template>
98+
<b-form @submit="onSubmit">
99+
<b-form-group id="exampleInputGroup1"
100+
label="Name"
101+
label-for="exampleInput1">
102+
<b-form-input id="exampleInput1"
103+
type="text"
104+
v-model="form.name"
105+
v-validate="{required: true, min:2}"
106+
:state="validateState('form.name')"
107+
aria-describedby="input1LiveFeedback"
108+
placeholder="Enter name" />
109+
<b-form-invalid-feedback id="input1LiveFeedback">
110+
This is a required field and must be at least 3 characters
111+
</b-form-invalid-feedback>
112+
</b-form-group>
113+
<b-form-group id="exampleInputGroup2"
114+
label="Food"
115+
label-for="exampleInput2">
116+
<b-form-select id="exampleInput2"
117+
:options="foods"
118+
v-validate="{required: true}"
119+
:state="validateState('form.foods')"
120+
v-model="form.food" />
121+
<b-form-invalid-feedback id="input2LiveFeedback">
122+
This is a required field
123+
</b-form-invalid-feedback>
124+
</b-form-group>
125+
<b-button type="submit"
126+
variant="primary"
127+
:disabled="form.errors.any()">
128+
Submit
129+
</b-button>
130+
</b-form>
131+
</template>
132+
133+
<script>
134+
135+
export default {
136+
name: "myForm",
137+
data() {
138+
return {
139+
foods: [
140+
"apple",
141+
"orange"
142+
],
143+
form: {}
144+
}
145+
},
146+
methods: {
147+
onSubmit() {
148+
// form submit logic
149+
},
150+
validateState(ref) {
151+
if (this.fields[ref] && this.fields[ref].dirty) {
152+
return !this.errors.has(ref)
153+
}
154+
return null
155+
},
156+
}
157+
}
158+
</script>
159+
160+
<!-- form-validation-1.vue -->
161+
```
162+

0 commit comments

Comments
 (0)