vue.js - 多个验证插件问题
问题描述
我正在做一个项目,我们称它为 ABC 项目。
该项目使用 Nuxt 作为前端,并使用vuelidate
andvuelidate-error-extractor
进行验证。
这个项目有一个我创建的自定义模块,它也有vuelidate
和vuelidate-error-extractor
作为依赖项。
在 nuxt 项目中,有一个vuelidate.js
我创建的插件 ( )。它具有与 ABC 项目相关的所有消息和属性。
这是vulidate.js
我的 Nuxt 项目中的文件
import Vue from 'vue'
import Vuelidate from 'vuelidate'
import VuelidateErrorExtractor, { templates } from 'vuelidate-error-extractor'
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import FormGroup from '@/components/mm/common/base/FormGroup'
import FormSummary from '@/components/mm/common/base/FormSummary'
const messages = {
required: '{attribute} is required.',
requiredCustom: '{attribute} is required.',
notColombo: '{attribute} must not be Colombo',
email: '{attribute} should be a valid email.',
maxLength: '{attribute} should be less than {max} characters.',
onlynumbers: '{attribute} should be numbers.',
internationalTpNumber: 'Maximum length of a phone number is 15 digits',
fullNameErrorPassport: 'Full Name is required',
noFutureDate: '{attribute} must not be set in the future.',
minTransactionDate: '{attribute} must be set on or before 01-January-2008',
startDate: 'Date of Birth',
startDateValidate: 'Start date must be greater than registration date and cannot be future date.',
}
Vue.use(BootstrapVue)
Vue.use(Vuelidate)
Vue.use(VuelidateErrorExtractor, {
messages,
attributes: {
districtId: 'District',
address1: 'Address',
postalCode: 'Postal Code',
village: 'Village',
district: 'District',
proposedName: 'Proposed Name',
selectedcountry: 'Country',
name: 'Name',
contactNumber: 'Contact Number',
email: 'Email Address',
identityNumber: 'Identification Document Number',
dateOfChange: 'Date of Change',
dob: 'Date of Birth',
fullName: 'Full Name',
occupationId: 'Occupation',
nationalityId: 'Nationality',
phoneNumber: 'Phone Number',
countryCode: 'Country Code',
gender: 'Gender',
retirementDate: 'Retirement Date',
startDate: 'Date of Birth'
}
})
Vue.component('FormGroup', FormGroup)
Vue.component('FormSummary', FormSummary)
Vue.component('formWrapper', templates.FormWrapper)
该模块还有一个名为的自定义插件vuelidate.js
,看起来像 Nuxt 项目中的插件。
唯一的变化是模块在vulidate.js
文件中有一组不同的属性和消息。
问题是模块覆盖了 Nuxt 的vuelidate
文件,这意味着 Nuxt 项目中的表单显示了一些来自模块而不是来自 Nuxt 项目的错误消息。
我们可以设置插件的范围吗?
我们可以阻止 Nuxt 项目使用来自模块的错误消息,而是使用来自 Nuxt 项目的错误消息吗?
这是我vuelidate.js
在模块中的文件
import Vue from 'vue'
import Vuelidate from 'vuelidate'
import VuelidateErrorExtractor, { templates } from 'vuelidate-error-extractor'
import BootstrapVue from 'bootstrap-vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import FormGroup from '@/components/mm/common/base/FormGroup'
import FormSummary from '@/components/mm/common/base/FormSummary'
const messages = {
required: '{attribute} is required.',
requiredCustom: '{attribute} is required.',
notColombo: '{attribute} must not be Colombo',
email: '{attribute} should be a valid email.',
maxLength: '{attribute} should be less than {max} characters.',
onlynumbers: '{attribute} should be numbers.',
internationalTpNumber: 'Maximum length of a phone number is 15 digits',
fullNameErrorPassport: 'Full Name is required',
noFutureDate: '{attribute} must not be set in the future.',
minTransactionDate: '{attribute} must be set on or before 01-January-2008',
startDate: 'Date of Birth',
startDateValidate: 'Start date must be greater than registration date and cannot be future date.',
}
Vue.use(BootstrapVue)
Vue.use(Vuelidate)
Vue.use(VuelidateErrorExtractor, {
messages,
attributes: {
dob: 'Date of Birth',
fullName: 'Full Name',
occupationId: 'Occupation',
nationalityId: 'Nationality',
phoneNumber: 'Phone Number',
age:"Age"
countryCode: 'Country Code',
gender: 'Gender',
retirementDate: 'Retirement Date',
startDate: 'Date of Birth'
}
})
Vue.component('FormGroup', FormGroup)
Vue.component('FormSummary', FormSummary)
Vue.component('formWrapper', templates.FormWrapper)
解决方案
推荐阅读
- python - NameError 没有用 Python 中的 Json 和 Pandas 定义
- android - 了解发布促销和评论
- mongodb - mongodb中的自定义orderin
- angular - 如何使用 Renderer2 使 parentNode 和模拟点击更安全?
- azure - 为什么 Azure 服务总线消息处理(AKS 中托管的 .NET Core)每 15 分钟下降一次,并且需要 3 倍以前的群集资源?
- sql - 将两个子查询合并为一列
- solr - Apache solr 在 Linux 中的自定义文件夹中安装 - 404 问题
- r - 如何在 R 中为 dygraph 鼠标悬停添加标签?
- javascript - 如何在 JS 中更新 typeahead 中的数据源?
- python - 如何仅屏蔽输出(标记数据)。我的输入数据没有任何问题