Skip to content

Added vee-validate example #1889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions docs/markdown/reference/validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This is a verbose example designed to show how Bootstrap-Vue and Vuelidate inter
<b-form-input id="exampleInput1"
type="text"
v-model="form.name"
:state="!$v.form.name.$invalid"
:state="$v.form.name.$dirty ? !$v.name.$error : null"
aria-describedby="input1LiveFeedback"
placeholder="Enter name" />
<b-form-invalid-feedback id="input1LiveFeedback">
Expand All @@ -33,7 +33,7 @@ This is a verbose example designed to show how Bootstrap-Vue and Vuelidate inter
label-for="exampleInput2">
<b-form-select id="exampleInput2"
:options="foods"
:state="!$v.form.food.$invalid"
:state="$v.form.food.$dirty ? !$v.name.$error : null"
v-model="form.food" />
<b-form-invalid-feedback id="input2LiveFeedback">
This is a required field
Expand Down Expand Up @@ -86,3 +86,77 @@ This is a verbose example designed to show how Bootstrap-Vue and Vuelidate inter

<!-- form-validation-1.vue -->
```

## vee-validate

[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.

Same example as above just modified for vee-validate:

```html
<template>
<b-form @submit="onSubmit">
<b-form-group id="exampleInputGroup1"
label="Name"
label-for="exampleInput1">
<b-form-input id="exampleInput1"
type="text"
v-model="form.name"
v-validate="{required: true, min:2}"
:state="validateState('form.name')"
aria-describedby="input1LiveFeedback"
placeholder="Enter name" />
<b-form-invalid-feedback id="input1LiveFeedback">
This is a required field and must be at least 3 characters
</b-form-invalid-feedback>
</b-form-group>
<b-form-group id="exampleInputGroup2"
label="Food"
label-for="exampleInput2">
<b-form-select id="exampleInput2"
:options="foods"
v-validate="{required: true}"
:state="validateState('form.foods')"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where this method from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing. I wrote it, but it didn't end up in the patch. I've updated the doc to include it.

v-model="form.food" />
<b-form-invalid-feedback id="input2LiveFeedback">
This is a required field
</b-form-invalid-feedback>
</b-form-group>
<b-button type="submit"
variant="primary"
:disabled="form.errors.any()">
Submit
</b-button>
</b-form>
</template>

<script>

export default {
name: "myForm",
data() {
return {
foods: [
"apple",
"orange"
],
form: {}
}
},
methods: {
onSubmit() {
// form submit logic
},
validateState(ref) {
if (this.fields[ref] && this.fields[ref].dirty) {
return !this.errors.has(ref)
}
return null
},
}
}
</script>

<!-- form-validation-1.vue -->
```