iView——表单校验是否为空填坑

iView Form 表单校验注意点

设置 rules, 并制定 prop

<Form ref="topicForm" :model="topicForm" :rules="topicFormValidate" :label-width="80">
  <FormItem label="名称" prop="topicName">
    <Input
           type="text"
           v-model="topicForm.topicName"
           placeholder="请输入名称"
           style="width: 180px"
           />
  </FormItem>
</Form>

代码校验与重置

// 校验
this.$refs.topicForm.validate(valid => {
 if (valid) {
   ...
 }
}
// 重置
this.$refs.topicForm.resetFields();
自定义校验
export default {
  name: "add-topic",
  data() {
    const validateName = (rule, value, callback) => {
      if (value) {
        checkTopicName(value)
          .then(() => {
          callback();
        })
          .catch(() => {
          callback(new Error("名称重复"));
        });
      } else {
        callback();
      }
    };
    return {
      topicName: [
        {required: true, message: "名称不能为空", trigger: "blur"},
        {validator: validateName, trigger: "blur"},
      ]
    }
  }
}

指定长度

topicName: [
  {required: true, message: "名称不能为空", trigger: "blur"},
  {validator: validateName, trigger: "blur"},
  {
    min: 6,
    max: 36,
    message: '专题名称长度为 6~36 位'
    }
]

指明数据类型 type
有时候,明明写了值,但是提示不能为空,比方说 select 组件就会有这个问题。

<FormItem label="模板" prop="templateId" v-show="createMode === 'create'">
  <Select
          v-model="topicForm.templateId"
          style="width: 180px"
          @on-change="onTemplateChange"
          clearable
          filterable
          >
    <Option
            v-for="item in templateList"
            :value="item.id"
            :key="item.id"
            >{{ item.name }}
    </Option>
  </Select>

这是因为默认校验数据类型为String,而实际用到的 value 是数字,所以需要制定 type

{required: true, type: “number”, message: “模板不能为空”, trigger: “blur”},
触发条件
有时只是用 blur 不一定能满发需求,比方说这种直接复制的,可以添加触发条件 change。或者 select 组件已经选了值,还是提示不能为空,点了提交按钮才重新校验,这也可以通过添加 change 触发器解决。

image-20190806145320917

Iview 不支持直接给 trigger 赋值数组,需要分开写:

templateId: [
  {required: true, type: "number", message: "模板不能为空", trigger: "blur"},
  {required: true, type: "number", message: "模板不能为空", trigger: "change"},
]


动态增加校验项
if (select.value === "copy") {
  this.$delete(this.topicFormValidate, "templateId");
  this.$set(this.topicFormValidate, "copyId", [
    {required: true, message: "ID 不能为空", trigger: "blur"},
    {required: true, message: "ID 不能为空", trigger: "change"}
  ]);
} else if (select.value === "create") {
  this.$delete(this.topicFormValidate, "copyId");
  this.$set(this.topicFormValidate, "templateId", [
    {required: true, type: 'number', message: "模板不能为空", trigger: "blur"},
    {required: true, type: 'number', message: "模板不能为空", trigger: "change"},
  ]);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Uni-app 中,可以通过在 `input` 组件上绑定 `@blur` 事件,在事件处理函数中进行校验校验规则可以通过编写自定义方法来实现。 下面是一个示例代码,演示了如何自定义 `uni-form` 组件的校验规则。 ``` html <template> <view class="uni-form"> <view class="uni-form-item"> <view class="uni-form-label">用户名</view> <view class="uni-form-input"> <input v-model="username" @blur="checkUsername" /> </view> <view class="uni-form-msg">{{ usernameMsg }}</view> </view> <view class="uni-form-item"> <view class="uni-form-label">密码</view> <view class="uni-form-input"> <input type="password" v-model="password" @blur="checkPassword" /> </view> <view class="uni-form-msg">{{ passwordMsg }}</view> </view> </view> </template> <script> export default { data() { return { username: '', password: '', usernameMsg: '', passwordMsg: '', }; }, methods: { checkUsername() { if (!this.username) { this.usernameMsg = '用户名不能为空'; } else { this.usernameMsg = ''; } }, checkPassword() { if (!this.password) { this.passwordMsg = '密码不能为空'; } else if (this.password.length < 6) { this.passwordMsg = '密码长度不能小于6位'; } else { this.passwordMsg = ''; } }, }, }; </script> ``` 在上面的代码中,我们定义了两个输入框,分别用于输入用户名和密码。在每个输入框上,我们都绑定了 `@blur` 事件,用于在输入框失去焦点后校验输入内容。校验规则定义在 `checkUsername` 和 `checkPassword` 方法中,根据输入内容的不同,这些方法会设置不同的提示信息,将其显示在对应的提示框中。 当用户提交表单时,我们只需要检查提示信息是否为空,如果不为空,说明输入内容有误,需要提示用户重新输入。如果所有提示信息都为空,说明输入内容符合要求,可以提交表单

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值