【问题标题】:custom validation message in vee-validatevee-validate 中的自定义验证消息
【发布时间】:2020-01-15 18:50:11
【问题描述】:

我正在使用 Laravel - 5.8 和 Vue.js。我的问题是关于如何在 Vee-Validate 库中显示规则的自定义错误消息。我没有显示“必需”规则的自定义消息,而是显示:“first_name 字段是必需的。”预期的消息是“请输入名字。”

以下代码在 app.js 中

import { ValidationProvider } from 'vee-validate/dist/vee-validate.full';

这是我的 HTML 组件代码。

<template>    
    <div>
        <form role="form">
            <ValidationProvider name="first_name" :rules="required">
                <div slot-scope="{ errors }">
                    <input v-model="profileForm.first_name" class="form-control">
                    <p>{{ errors[0] }}</p>
                </div>
            </ValidationProvider>
                  
            <button type="button" @click="validateBeforeSubmit()">Update Profile</button>
        </form>
    </div>
</template>

下面是我的 JS 脚本代码

<script>
    import { localize } from 'vee-validate/dist/vee-validate.full';
    import en from "vee-validate/dist/locale/en.json";

    export default {
        data() {
            return {
                profileForm: {
                    first_name: ''
                },
                customMessages: {
                    en: {
                        custom: {
                            'first_name': {
                                required: 'Please enter first name'
                            }
                        }
                    }
                },
            }
        },
        created() {
            localize("en", this.customMessages);
        },
        methods: {
            validateBeforeSubmit() {
                this.$validator.validateAll();
            }
        }
    }
</script>

我错过了什么吗?

【问题讨论】:

    标签: laravel vue.js vue-component laravel-5.8 vee-validate


    【解决方案1】:

    一种通用的方法是使用extend()

    import { extend } from 'vee-validate';
    import { required } from 'vee-validate/dist/rules';
    
    extend('required', {
        ...required,
        message: 'Please enter first name',
    });
    

    这将传播并包含规则的所有默认属性,同时仍允许您添加自己的自定义消息。

    i18n 示例:

    import { extend } from 'vee-validate';
    import { required } from 'vee-validate/dist/rules';
    import i18n from 'localization';
    
    extend('required', {
        ...required,
        message: i18n.t('LOCALIZATION_PATH'),
    });
    

    【讨论】:

      【解决方案2】:

      custom 关键字已在版本 3 中删除。现在替换为 fieldsAlso this info was missing in docs

      For more info please follow this issue link on github

      【讨论】:

        【解决方案3】:

        非常简单的一个。虽然不好,但是很管用。

        <input name="yourFieldName" type="text" class="form-control" v-model="yourFieldName" v-validate="'alpha_spaces'">
        
        <span class="small text-danger">{{ errors.first('yourFieldName')?'Your custome message':'' }}</span>
        

        【讨论】:

          【解决方案4】:

          感谢@pankaj 提供正确答案。对于那些懒得关注链接的人,文档指出了以下解决方案:

          import { localize } from 'vee-validate';
          
          localize({
            en: {
              messages: {
                // generic rule messages...
              },
              fields: {
                password: {
                  required: 'Password cannot be empty!',
                  max: 'Are you really going to remember that?',
                  min: 'Too few, you want to get doxed?'
                }
              }
            }
          });
          

          【讨论】:

            【解决方案5】:

            我认为您需要将this.customMessages.en 传递给localize() 或者传递的对象具有顶级属性en。传递的字典应该是only contain your custom messages

            【讨论】:

            • 我现在有这个代码:localize("en", this.customMessages.en);。这仍然没有显示我的自定义消息
            【解决方案6】:

            如@zcoop98 所示,可以通过“扩展”添加自定义消息。需要注意的是,如果我们像这样使用它message: () =&gt; i18n.t ('LOCALIZATION_PATH')

            【讨论】:

            猜你喜欢
            • 2018-11-25
            • 2020-06-30
            • 2019-09-23
            • 2021-12-07
            • 1970-01-01
            • 1970-01-01
            • 2018-08-19
            • 2018-03-01
            • 2021-07-04
            相关资源
            最近更新 更多