• 使用iview框架,如何进行输入框或者按钮的关联验证


    iview框架的Form 组件基于 async-validator 实现数据验证,给 Form 设置属性 rules,同时给需要验证的 FormItem 设置属性 prop 指向对应字段即可。

    简单的验证

    <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,可以自定义验证规则

    <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' } ] }
  • 相关阅读:
    ASP.NET中POST提交数据并跳转页面
    kindeditor编辑器图片水印
    jquery live hover绑定方法
    ASP.NET MVC实现多个按钮提交事件
    Asp.Net时间戳与时间互转
    Django-管理站点重写admin模板
    Pycharm快捷键整理(Mac)
    Python 调用datetime或者time获取时间的时候以及时间转换,最好设置一下时区 否则会出现相差8个小时的情况
    django中使用原生sql
    [django]用日期来查询datetime类型字段
  • 原文地址:https://www.cnblogs.com/snowRock/p/10637330.html
Copyright © 2020-2023  润新知