【问题标题】:With vee-validate make custom validation使用 vee-validate 进行自定义验证
【发布时间】:2019-09-23 17:41:41
【问题描述】:

在我的 vue 2.5.17 应用程序中,我有一个选择输入和文本输入,并且我创建了一个验证规则,其中只有一个必须填写:

    <div class="form-row p-3">
        <label for="existing_user_list_id" class="col-12 col-sm-4 col-form-label">Select already existing list</label>
        <div class="col-12 col-sm-8">
            <v-select
                    v-model="selection_existing_user_list_id"
                    data-vv-name="selection_existing_user_list_id"
                    :options="userListsByUserIdArray"
                    v-validate="''"
                    id="existing_user_list_id"
                    name="existing_user_list_id"
                    class="form-control editable_field"
                    placeholder="Select existing user list"
                    v-on:input="onChangeSelectionExistingUserListId($event);"
            ></v-select>
            <span v-show="vueErrorsList.has('existing_user_list_id')" class="text-danger">{{ vueErrorsList.first('existing_user_list_id') }}</span>
        </div>
    </div>

    <div class="form-row p-1 m-1 ml-4">
        <strong>OR</strong>
    </div>

    <div class="form-row p-2`">
        <label for="new_user_list_title" class="col-12 col-sm-4 col-form-label">Create a new list</label>
        <div class="col-12 col-sm-8">
            <input class="form-control" value="" id="new_user_list_title" v-model="new_user_list_title" @change="onChangeBewUserListTitle($event);">
            <span v-show="vueErrorsList.has('title')" class="text-danger">{{ vueErrorsList.first('title') }}</span>
        </div>
    </div>

我发现了这个:
https://baianat.github.io/vee-validate/guide/custom-rules.html#creating-a-custom-rule

但我很困惑如何在我的情况下使用它。我试试:

    import { Validator } from 'vee-validate';
    Validator.extend('new_user_list_title', {
        getMessage: field => 'The ' + field + ' value is not a valid new_user_list_title.',
        validate: value => false // for simplicity I try to raise error always
//        validate: value => value.length == 0 && selection_existing_user_list_id == null
    })

但我的自定义错误永远不会触发,即使我设置始终为 false,

哪个是正确的方法?

谢谢!

【问题讨论】:

    标签: vuejs2 vee-validate


    【解决方案1】:

    添加自定义规则后,需要在组件中使用(v-validate):

    <input 
      class="form-control" 
      id="new_user_list_title" 
      v-model="new_user_list_title" 
      @change="onChangeBewUserListTitle($event);"
      v-validate="'new_user_list_title'">
    

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2020-01-15
      • 2021-11-17
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 2020-12-20
      • 2018-03-01
      • 2020-12-20
      相关资源
      最近更新 更多