【问题标题】:Validate a 'required' field using a dynamic validation schema使用动态验证模式验证“必填”字段
【发布时间】:2021-11-01 01:50:38
【问题描述】:

project_id 需要在表单提交时进行验证:

<td>
    <select2 
        name="project_id" 
        id="project_id"
        v-model.trim="item.project_id.$model"
        v-bind:class="{
            'is-invalid': item.project_id.$error,
            'is-valid':
            item.project_id.$dirty &&
            !item.project_id.$error,
        }"
    >
        <option value="">Select</option>
        <option v-for="(project) in projects" 
            :selected="project.id == timesheet.project_id" 
            :key="project.id" 
            :value="project.id"
        >{{ project.name }}
        </option>
    </select2>
</td>
<td><input type= "checkbox" name="disable" v-on:click="disabled()"></td>
<script>
    import { required, minLength } from "vuelidate/lib/validators";
    validations() {
        return{
            timesheet: {
                items: {
                    required,
                    minLength: minLength(1),
                    project_id: { required },
                }
            }
        }
    },
    disabled(){
        $('#project_id').attr('disabled', true);
    }
</script>

disable复选框被选中时,如何使该字段成为非必填项? 我尝试使用 requiredIf 验证器,但似乎在某处丢失:

            $each: {
                project_id: { 
                    required: requiredIf (function(item){
                        return this.disable
                    })
                },
            },

【问题讨论】:

    标签: jquery vue.js validation vuelidate


    【解决方案1】:

    您可能尝试在 disable() 函数中执行以下操作:

    document.getElementById("project_id").removeAttribute("required");
    // or
    document.getElementById("project_id").setAttribute("required", "");
    

    要决定是否需要删除属性,您需要将v-model 属性应用于&lt;input type= "checkbox" name="disable" v-on:click="disabled()"&gt;。如果选中此复选框,则其属性将变为 true,否则将变为 false


    编辑:这是一个完整的用法示例。

    <template>
      <div>
        <select id="project_id" required>
          <option value="bar">Option 1</option>
          <option value="foo">Option 2</option>
          <option value="baz">Option 3</option>
        </select>
        <input type="checkbox" name="disable" v-model="clickedBox" @change="checkSelected">
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          clickedBox: false
        }
      },
      methods: {
        checkSelected() {
          if (this.clickedBox === true) {
            document.getElementById("project_id").removeAttribute("required");
          }
          else {
            document.getElementById("project_id").setAttribute("required", "");
          }
        }
      }
    }
    </script>
    

    如果选中该复选框project_id 将失去它的required 属性。如果未选中,required 属性将被重新设置。


    EDIT2:使用动态验证架构

    如果您想对project_id 使用动态验证架构,您需要像这样使用它:

    validations() {
      if (!this.clickedBox) {
        return {
          timesheet: {
            items: {
              required,
              minLength: minLength(1),
              project_id: { required },
            }
          }
        }
      } else {
        return {
          timesheet: {
            items: {
              required,
              minLength: minLength(1),
              project_id: ''
            }
          }
        }
      }
    }
    

    有关详细信息,请参阅文档:Dynamic validation schema

    【讨论】:

    • 您的回答几乎是正确的,先生,我非常感谢。
    • 但是,我正在寻找如何使用 Vuelidate 'required' 验证器来执行此操作的解决方案。
    • @user16780927 您的问题不清楚,因为您询问如何使该字段成为非必需字段。现在我知道你在谈论你的架构,我编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多