【问题标题】:Infinite targets of custom rule is not working自定义规则的无限目标不起作用
【发布时间】:2020-09-28 13:16:47
【问题描述】:

js

VeeValidate.Validator.extend('requireWithoutAll', {
    validate: (value, [ ...target ]) => {
    return value && _.compact(target).length;
  },
  getMessage: (field, [...target]) => 'The ${field} is required when none of ${target} are present.'
}, {
    hasTarget: true
});

vue

                        <ValidationProvider name="name1" vid="ref1">
                            <el-form-item slot-scope="{ errors }" :error="errors[0]" label="Lorum Ipsum 1">                                
                                <el-checkbox v-model="is_model_1">Yes</el-checkbox>
                            </el-form-item>    
                        </ValidationProvider>    
                        <ValidationProvider name="name2" vid="ref2">    
                            <el-form-item slot-scope="{ errors }" :error="errors[0]" label="Lorum Ipsum 2">                                
                                <el-checkbox v-model="is_model_2">Yes</el-checkbox>
                            </el-form-item>    
                        </ValidationProvider>    
                        <ValidationProvider :rules="'requireWithoutAll:ref1,ref2'" name="name3" vid="ref3">    
                            <el-form-item slot-scope="{ errors }" :error="errors[0]" label="Lorum Ipsum 3">                                
                                    <el-checkbox v-model="is_model_3">Yes</el-checkbox>
                            </el-form-item>    
                        </ValidationProvider>

预期结果ref1 & ref2 没有选择时,ref3 会产生错误。

实际结果 不会产生错误。

【问题讨论】:

  • 什么是ref1、ref2和ref3?如果您不提供完整的问题,就很难猜出您在问什么。
  • 我提供了整个代码

标签: vue.js vee-validate


【解决方案1】:

指定目标的格式是rules="requireWithoutAll:@ref1,@ref2"而不是rules="requireWithoutAll:ref1,ref2"

在此处查看工作示例:https://codesandbox.io/s/codesandbox-forked-u0s3g?file=/src/Demo.vue

相关代码:

  <ValidationProvider
    rules="requireWithoutAll:@ref1,@ref2"
    vid="ref3"
    v-slot="{ errors }"
    tag="div"
  >
     <!-- your checkbox UI here -->
  </ValidationProvider>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 2015-12-26
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多