简体   繁体   English

在 Quasar 中实现双重验证的最佳方法是什么?

[英]What is the best way to implement double validation in Quasar?

I want to allow either the checkbox to be checked, the text field to be filled in, or both in Quasar.我想在 Quasar 中允许选中复选框、填写文本字段或两者。
In the validation of this form, the error does not disappear even if the check box is checked after the validation error occurs.在此表单的验证中,即使在验证错误发生后选中复选框,错误也不会消失。

CodePen代码笔

index.html索引.html

<div id="q-app">
  <q-card class="my-card q-py-md">
    <q-card-section>
      <h3 class="text-h5 q-my-none">Fruits<span class="text-red">*</span></h3>
      <p>Which fruit did you eat?</p>
    </q-card-section>
  <q-card-section>
    <q-field
      hide-bottom-space
      borderless
      item-aligned
      class="row q-pa-none"
      :rules="[fruitsRule]">
        <q-option-group
          :options="fruits"
          label="Notifications"
          type="checkbox"
          v-model="fruits_select"></q-option-group>
      </q-field>
      <q-input
        class="q-mt-md"
        v-model="fruits_other"
        label="Other"
        :rules="[fruitsRule]"></q-input>
    </q-card-section>
  </q-card>
</div>

main.js main.js

new Vue({
  el: '#q-app',
  data () {
    return {
      fruits_select: [],
      fruits: [
        { label: 'Apple', value: 1},
        { label: 'Banana', value: 2},
        { label: 'Orange', value: 3}
      ],
      fruits_other: ""
    }
  },

  methods: {
    fruitsRule () {
      if (!this.fruits_select.length && this.fruits_other === ""){
        return 'Select one or more checkboxes or fill in the others.'
      }
    }
  }
})

My question is我的问题是

  1. How Can I fix the checkbox error?如何修复复选框错误?
  2. Is this double validation the best way?这种双重验证是最好的方法吗?

There are few problems with your code and Quasar validation in general一般而言,您的代码和 Quasar 验证几乎没有问题

  1. Your checkboxes are not triggering validation at all.您的复选框根本不会触发验证。 The reason is you are missing v-model on q-field .原因是您在q-field上缺少v-model Without it, the rules are not executed for checkboxes没有它,复选框不会执行规则
<q-field hide-bottom-space borderless item-aligned class="row q-pa-none" v-model="fruits_select" :rules="[fruitsRule]">
  <q-option-group :options="fruits" label="Notifications" type="checkbox" v-model="fruits_select" />
</q-field>
  1. Your custom rule also seems wrong.您的自定义规则似乎也错误。 According to the docs:根据文档:

Your custom validator will be a function which returns true if validator succeeds or String with error message if it doesn't succeed您的自定义验证器将是 function 如果验证器成功则返回true ,如果不成功则返回带有错误消息的String

But your fruitsRule never retuns true .但是您的fruitsRule永远不会返回true Fix:使固定:

fruitsRule () {
  return this.fruits_select.length > 0 || this.fruits_other !== "" || 'Select one or more checkboxes or fill in the others.'
}
  1. Quasar always check the rules only for the component which model has changed. Quasar 始终只检查 model 已更改的组件的规则。 So when you change the value in textbox, rules for checkboxes are not re-evaluated因此,当您更改文本框中的值时,不会重新评估复选框的规则

Easiest thing to fix this is to use <QForm> , which has a method validate() that triggers the validation of every field in the form and execute it whenever one of the models changes:解决这个问题最简单的方法是使用<QForm> ,它有一个方法validate()触发表单中每个字段的验证,并在其中一个模型更改时执行它:

<q-form ref="form">
  <q-field hide-bottom-space borderless item-aligned class="row q-pa-none" v-model="fruits_select" :rules="[fruitsRule]">
    <q-option-group :options="fruits" label="Notifications" type="checkbox" v-model="fruits_select" />
  </q-field>
  <q-input class="q-mt-md" v-model="fruits_other" label="Other" :rules="[fruitsRule]" />
</q-form>
watch: {
    fruits_select() {
      this.$refs.form.validate()
    },
    fruits_other() {
      this.$refs.form.validate()
    }
  },

Using QForm makes sense when you have more controls.当您拥有更多控件时,使用QForm是有意义的。 You have only two so of course, you can leave out the QForm , place refs directly on both controls and use watchers to trigger the validation of the other control.你只有两个,所以当然,你可以省略QForm ,直接在两个控件上放置refs并使用观察者来触发另一个控件的验证。

It is not very good solution but unfortunately the Quasar's internal validation is pretty limited.这不是很好的解决方案,但不幸的是 Quasar 的内部验证非常有限。 If you forms gets little bit more complicated I would recommend using external validation library - for examle Vuelidate如果您 forms 变得有点复杂,我建议您使用外部验证库 - 例如 Vuelidate

Demo演示

UPDATE: Updated my demo so all models are grouped together to one object.更新:更新了我的演示,因此所有模型都归为一个 object。 This allows only use single deep watcher to trigger all validations...这仅允许使用单个deep观察者来触发所有验证......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM