@@ -125,30 +125,30 @@ Change the alignment of labels
125125
126126Form component allows you to verify your input data.
127127
128- To validate form item just add the ` rules ` attribute for ` vue-form ` to pass validation rules, and set ` field ` attribute for ` vue-form-item ` as a specific key that needs to be validated .
128+ To validate form item just add the ` v-validate ` attribute for Input, Select, Radio or Checkbox components to pass validation rules .
129129
130- See more information at [ async-validator ] ( https://github.com/yiminghe/async-validator ) .
130+ See more information at [ VeeValidate ] ( https://baianat. github.io/vee-validate/guide/rules.html?ref=vfc ) .
131131
132132``` example
133133<template>
134- <vue-form :model="form" :rules="formRules" style="width: 500px;" label-position="right" label-width="150px" ref="form">
134+ <vue-form :model="form" style="width: 500px;" label-position="right" label-width="150px" ref="form">
135135 <vue-form-item field="email" label="Email">
136- <vue-input v-model="form.email" style="width: 100%"></vue-input>
136+ <vue-input v-model="form.email" name="email" v-validate="'required|email'" style="width: 100%"></vue-input>
137137 </vue-form-item>
138138 <vue-form-item field="password" label="Password">
139- <vue-input v-model="form.password"></vue-input>
139+ <vue-input v-model="form.password" name="password" v-validate="'required'" ></vue-input>
140140 </vue-form-item>
141141 <vue-form-item field="delivery" label="Delivery">
142- <vue-radio v-model="form.delivery" value="1">Radio 1</vue-radio>
143- <vue-radio v-model="form.delivery" value="2">Radio 2</vue-radio>
142+ <vue-radio v-model="form.delivery" name="delivery" v-validate="'required'" value="1">Radio 1</vue-radio>
143+ <vue-radio v-model="form.delivery" name="delivery" v-validate="'required'" value="2">Radio 2</vue-radio>
144144 </vue-form-item>
145145 <vue-form-item field="city" label="City">
146- <vue-select v-model="form.city" :data="options" placeholder="Select">
146+ <vue-select v-model="form.city" :data="options" name="city" v-validate="'required'" placeholder="Select">
147147 <vue-option v-for="i in options" :key="i.value" :value="i.value" :label="i.label"></vue-option>
148148 </vue-select>
149149 </vue-form-item>
150- <vue-form-item field="terms" label="Terms">
151- <vue-checkbox v-model="form.terms">I'm agree</vue-checkbox>
150+ <vue-form-item field="terms" label="Terms">
151+ <vue-checkbox v-model="form.terms" name="terms" v-validate="'required:true'" >I'm agree</vue-checkbox>
152152 </vue-form-item>
153153 <vue-form-item>
154154 <vue-button @click="onReset">Reset</vue-button>
@@ -167,41 +167,23 @@ See more information at [async-validator](https://github.com/yiminghe/async-vali
167167 terms: false,
168168 delivery: '',
169169 city: '',
170- },
171- formRules: {
172- email: [
173- { required: true, message: 'Email is required' },
174- { type: 'email', message: 'Email is not correct' }
175- ],
176- password: [
177- { required: true, message: 'Password is required' },
178- { min: 3, message: 'Password length must be greater than 3 characters' }
179- ],
180- terms: [
181- { pattern: /true/, message: 'Terms is required' }
182- ],
183- delivery: [
184- { required: true, message: 'Delivery is required' }
185- ],
186- city: [
187- { required: true, message: 'City is required' }
188- ]
189170 }
190171 }
191172 },
192173 methods: {
193174 async onSubmit () {
194- try {
195- await this.$refs.form.validate()
196- } catch (err) {
197- console.warn('Form is not valid')
198- }
175+ const res = await this.$validator.validate()
176+ if (res) alert('Form is valid')
199177 },
200178 onReset () {
201- for (let i in this.form) {
202- i === 'terms' ? this.form[i] = false : this.form[i] = ''
203- }
204- this.$refs.form.resetValidation()
179+ Object.keys(this.form).map(f => {
180+ if (typeof this.form[f] === 'boolean') {
181+ this.form[f] = false
182+ } else {
183+ this.form[f] = ''
184+ }
185+ })
186+ this.$validator.reset()
205187 }
206188 }
207189 }
@@ -213,18 +195,9 @@ See more information at [async-validator](https://github.com/yiminghe/async-vali
213195| Attributes | Description | Type | Accepted values | Default |
214196| ---------------- | --------------------------- | -------- | ---------------- | ------- |
215197| ` model ` | Data of form components | ` Object ` | - | - |
216- | ` rules ` | Validation rules of form | ` Object ` | - | - |
217198| ` label-position ` | Position of label form item | ` String ` | left, right, top | right |
218199| ` label-width ` | Width of label form item | ` String ` | | 100px |
219200
220- ## Form methods
221-
222- | Method | Description | Parameters |
223- | ------------------------------------- | ---------------------------------------- | ------------------------ |
224- | ` validate() ` | Validate all form item. Return ` Promise ` | |
225- | ` resetValidation() ` | Remove all validation result | |
226- | ` resetFieldValidation(field: string) ` | Remove field validation result | ` field ` - Field to reset |
227-
228201## Form item attributes
229202
230203| Attributes | Description | Type | Accepted values | Default |
0 commit comments