Skip to content

Commit

Permalink
feat: add vee-validate
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov committed Apr 8, 2019
1 parent f1f58b7 commit bce66ac
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 80 deletions.
9 changes: 9 additions & 0 deletions src/components/checkbox/Checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
export default {
name: 'VueCheckbox',
$_veeValidate: {
name () {
return this.name
},
value () {
return this.checked
}
},
model: {
prop: 'checked',
event: 'change'
Expand Down
9 changes: 9 additions & 0 deletions src/components/checkbox/CheckboxGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
export default {
name: 'VueCheckboxGroup',
$_veeValidate: {
name () {
return this.name
},
value () {
return this.modelValue
}
},
model: {
prop: 'modelValue',
event: 'change'
Expand Down
67 changes: 0 additions & 67 deletions src/components/form/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</template>

<script>
import AsyncValidator from 'async-validator'
export default {
name: 'VueForm',
Expand All @@ -28,10 +27,6 @@ export default {
type: Object,
default: () => {}
},
rules: {
type: Object,
default: () => {}
},
labelPosition: {
type: String,
default: 'right'
Expand All @@ -40,68 +35,6 @@ export default {
type: String,
default: '100px'
}
},
data () {
return {
validator: this.rules ? new AsyncValidator(this.rules) : undefined
}
},
methods: {
validate () {
return new Promise((resolve, reject) => {
console.warn(this.validator)
this.validator.validate(this.model, (err, f) => {
this.$children.forEach(child => {
if (!this.isChildFormItem(child)) return
this.setChildOptions(child, '', true, true)
})
if (!err) return resolve('Form is valid')
Object.keys(this.model).forEach(item => {
err.forEach(err => {
if (err.field === item) {
this.$children.forEach(child => {
if (!this.isChildFormItem(child)) return
if (child.field === item) {
this.setChildOptions(child, err.message, false, true)
}
})
}
})
return reject(new Error('Form is not valid'))
})
})
})
},
resetValidation () {
this.$children.forEach(child => {
if (!this.isChildFormItem(child)) return
this.setChildOptions(child, '', true, false)
})
},
resetFieldValidation (field) {
this.$children.forEach(child => {
if (!this.isChildFormItem(child)) return
if (child.field === field) {
this.setChildOptions(child, '', true, false)
}
})
},
isChildFormItem (child) {
return child.$options.name === 'VueFormItem'
},
setChildOptions (child, message, valid, state) {
child.validateMessage = message
child.isValid = valid
child.validateState = state
}
}
}
</script>
Expand Down
22 changes: 11 additions & 11 deletions src/components/form/FormItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<slot />
<transition name="form-slide-fade">
<div
v-if="!isValid"
v-if="showErrorMsg"
class="vue-form__item-error"
>
{{ validateMessage }}
{{ showErrorMsg }}
</div>
</transition>
</div>
Expand All @@ -34,24 +34,24 @@ export default {
type: String,
default: ''
},
rules: {
type: [Object, Array],
default: () => {}
},
field: {
type: String,
default: ''
}
},
data () {
return {
isValid: true,
validateMessage: '',
validateState: false
computed: {
showErrorMsg () {
let msg
if (this.errors.items.length) {
const item = this.errors.items.find(i => i.field === this.field)
if (item) msg = item.msg
}
return msg
}
}
}
</script>

<style lang="scss">
Expand Down
13 changes: 12 additions & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Select from './select/Select.vue'
import Option from './select/Option.vue'
import Form from './form/Form.vue'
import FormItem from './form/FormItem.vue'
import VeeValidate from 'vee-validate'

const components = [
Input,
Expand All @@ -21,7 +22,17 @@ const components = [
]

export default {
install (Vue, options) {
install (Vue, options = {}) {
let veeValidateOptions = {
events: 'change|input|blur'
}

if (options.veeValidate) {
veeValidateOptions = Object.assign(veeValidateOptions, options.veeValidate)
}

Vue.use(VeeValidate, veeValidateOptions)

components.forEach(component => {
Vue.component(component.name, component)
})
Expand Down
9 changes: 9 additions & 0 deletions src/components/input/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@
export default {
name: 'VueInput',
$_veeValidate: {
name () {
return this.name
},
value () {
return this.value
}
},
model: {
prop: 'value',
event: 'input'
Expand Down
11 changes: 10 additions & 1 deletion src/components/radio/Radio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@
export default {
name: 'VueRadio',
$_veeValidate: {
name () {
return this.name
},
value () {
return this.modelValue
}
},
model: {
prop: 'modelValue',
event: 'change'
},
props: {
modelValue: {
type: Object,
type: [String, Object],
default: () => {}
},
value: {
Expand Down
9 changes: 9 additions & 0 deletions src/components/select/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ import { clickOutside } from '../../utils/directives'
export default {
name: 'VueSelect',
$_veeValidate: {
name () {
return this.name
},
value () {
return this.value
}
},
components: {
[Input.name]: Input,
[Popper.name]: Popper
Expand Down

0 comments on commit bce66ac

Please sign in to comment.