iview 表单验证不通过问题?

项目需要,需要怂iview.。使用一段时间感觉跟elementUI用起来差不多很方便。使用过程中遇到表单验证问题,如何避免在验证过程中偶尔出现验证不通过的异常情况?

《1》:给 <Form> 标签设置属性 :model = "fromdata" 与 :rules = "fromrules" 与 ref = "fromdatadom"

《2》:同时给需要验证的每个 <FormItem> 设置属性 prop ,同时在fromrules中定义该属性的校验规则

《3》:将需要验证的每个属性值定义在fromdata内,并:model 绑定在 input上。必须设置,某个值改变验证依然通不过

《4》:在操作保存按钮时,通过ref = "fromdatadom"设置某个范围内验证,参数为检验完的回调,会返回一个 Boolean 表示成功与失败
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$Message.success('Success!');
        } else {
          this.$Message.error('Fail!');
        }
      })
    }

以上是验证的基本步骤,但是我做的过程中遇到的的问题是由于iview默认校验数据类型为string,而偶尔其他数据类型没注意。iview的type验证种类:

type: 'string', //iview默认校验数据类型为String
type: 'array',
type:'number'
type:'integer'  //Must be of type number and an integer.
type:'float'    //Must be of type number and a floating point number.
type:'boolean'
type:'object'   //Must be of type object and not Array.isArray.
type:'array'    //Must be an array as determined by Array.isArray.
type:'regexp'   //正则
type: 'email',
type: 'date', 		
type:'url'      //Must be of type url.
enum: Value must exist in the enum.
hex: Must be of type hex.

 更多iview验证详情见:https://github.com/yiminghe/async-validator

例子:

<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="电话" prop="tel">
            <Input v-model="formValidate.mail" placeholder="请输入号码"></Input>
        </FormItem>
        <FormItem label="邮箱" prop="mail">
            <Input v-model="formValidate.mail" placeholder="Enter your e-mail"></Input>
        </FormItem>
        <FormItem label="城市" prop="city">
            <Select v-model="formValidate.city" placeholder="Select your city">
                <Option value="beijing">New York</Option>
                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值