使用iview框架,如何進行輸入框或者按鈕的關聯驗證

iview框架的Form 組件基於 async-validator 實現數據驗證,給 Form 設置屬性 rules,同時給須要驗證的 FormItem 設置屬性 prop 指向對應字段便可。框架

簡單的驗證iview

<Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="姓名" prop="name">
            <Input v-model="formValidate.name" placeholder="輸入你的名字"></Input>
        </FormItem>
        <FormItem label="郵箱" prop="mail">
            <Input v-model="formValidate.mail" placeholder="輸入郵箱地址"></Input>
        </FormItem>
        <FormItem label="城市" prop="city">
            <Select v-model="formValidate.city" placeholder="選擇城市">
                <Option value="beijing">北京</Option>
                <Option value="shanghai">上海</Option>
                <Option value="shenzhen">深圳</Option>
            </Select>
        </FormItem>
        <FormItem label="性別" prop="gender">
            <RadioGroup v-model="formValidate.gender">
                <Radio label="male">男</Radio>
                <Radio label="female">女</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="愛好" prop="interest">
            <CheckboxGroup v-model="formValidate.interest">
                <Checkbox label="Eat"></Checkbox>
                <Checkbox label="Sleep"></Checkbox>
                <Checkbox label="Run"></Checkbox>
                <Checkbox label="Movie"></Checkbox>
            </CheckboxGroup>
        </FormItem>
        <FormItem label="我的簡介" prop="desc">
            <Input v-model="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="請輸入..."></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
        </FormItem>
</Form>
export default {
        data () {
            return {
                formValidate: {
                    name: '',
                    mail: '',
                    city: '',
                    gender: '',
                    interest: [],
                    desc: ''
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '不能爲空', trigger: 'blur' }
                    ],
                    mail: [
                        { required: true, message: '郵箱不能爲空', trigger: 'blur' },
                        { type: 'email', message: '格式錯誤', trigger: 'blur' }
                    ],
                    city: [
                        { required: true, message: '請選擇城市', trigger: 'change' }
                    ],
                    gender: [
                        { required: true, message: '請選擇性別', trigger: 'change' }
                    ],
                    interest: [
                        { required: true, type: 'array', min: 1, message: '請選擇興趣愛好', trigger: 'change' },
                        { type: 'array', max: 2, message: '最多選擇兩項', trigger: 'change' }
                    ],
                    desc: [
                        { required: true, message: '請輸入我的簡介', trigger: 'blur' },
                        { type: 'string', min: 20, message: '不能少於20個字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        //驗證成功後執行的方法
                    } else {
                        //驗證失敗後執行的方法
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            }
        }
}

 

下面是兩個輸入框之間關聯的例子,就拿最近作的一個項目舉例吧,async-validator有一個字段validator,能夠自定義驗證規則async

<Form ref="formRecord" :model="formRecord" label-position="right" :rules="ruleRecord">
  <FormItem prop="c6">
    <Input type="text" v-model="formRecord.c6"></Input>
  </FormItem>
   <FormItem prop="c7">
     <Input type="text" v-model="formRecord.c7"></Input>
   </FormItem>
</Form>
 
 
 let checkc7 = (rule, value, callback) => {
  let c7 = parseFloat(this.formRecord.c7)
  if (c7 < (c6 * 0.95)) {
    callback("實測製冷量不該小於額定製冷量的95%")
  } else {
  callback()
  }
  }

ruleRecord:{ c6: [ { required:
true, message: '不能爲空' } ], c7: [ { required: true, message: '不能爲空' }, { validator: checkc7, trigger: 'blur' } ] }
相關文章
相關標籤/搜索