对于我的 Vuejs 应用程序,我使用 Vee-validate进行验证。目前它将有效的 URL 标记为无效,例如 http://localhost:3000
。
这也发生在他们的样本上: http://vee-validate.logaretm.com/rules.html#rule-url
如果您在他们的示例中输入 http://localhost:3000
,您将看到消息 The field field is not a valid URL.
最佳答案
经过搜索,在Vee-validate's Github上找到了一些相关问题,但没有一个能完全解决我的问题。这是我必须做的才能让它验证本地主机 URL:
添加验证器包
npm install validator
添加新规则:
const urlFixRule = (value) => { return isURL(value, { require_tld: false }); };
应用新规则:
VeeValidate.Validator.extend('url', urlFixRule);
它在我的 js 文件中的样子
import Vue from 'vue';
import isURL from 'validator/lib/isURL';
import VeeValidate from 'vee-validate';
import App from './App';
// Form Validation (https://github.com/baianat/vee-validate)
Vue.use(VeeValidate);
// Temporary Fix for Vee-validate bug, until the next release
const urlFixRule = (value) => {
return isURL(value, {
require_tld: false
});
};
VeeValidate.Validator.extend('url', urlFixRule);
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
});
关于javascript - Vue Vee-validate 将有效的 URL 标记为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858242/