Versions
VueJs:2.3.3
Vee-Validate:2.0.0-rc.25
Description
我有一个自定义组件 . 这是一个带有characteres计数器的输入,我试图在这个输入中放入vee-validate . 我想在Form提交时显示错误 . 我遵循了vee-validate文档中的每一步,但它没有用 . 我的表单提交自己忽略任何输入的错误 .
Steps To Reproduce:
使用vee-validate创建自定义组件
CODE:
Parent.vue
<vue-input maxlength="20" minlength="3" placeholder="works"
validate="required|numeric" v-model="dataItem.teste" name="teste"></vue-input>
Component.js
Vue.component('vue-input', {
props: ['maxlength', 'minlength', 'placeholder', 'validate', 'value', 'name'],
template: `<div>
<div class="input-group">
<input type="text" :name="name" :value="value"
@input="$emit('input', $event.target.value);
counter = $event.target.value.length"
:maxlength="maxlength" :placeholder="placeholder"
v-validate="validate"
:class="{'is-danger': errors.has(name), 'form-control' : true}">
Erros: {{errors}}
<span class="input-group-addon">
{{ maxlength - counter }}
</span>
<span v-show="errors.has(name)" class="text-danger m-t-xs">
{{ errors.first(name) }}
</span>
</div>
</div>`,
data: () => ({
counter: 0
})
});
1 回答
您可以在子组件中使用父
$validator
.请参阅子组件中的
created()
hook:Children.vue
Parent.vue
有2个更好的支持解决方案,但您需要在
Parent.vue
组件中移动<span>
并进行验证,因为errors
/$validator
将在Children.vue
组件中可用 NOT .另外,我是 not 能够在同一表单中使用相同名称的多个输入组件,但您可以查看它们并测试它:
Solution 1
你可以使用Vue的provide/inject API:
Children.vue
Solution 2
使用VeeValidate的constructor options:
Children.vue