admin管理员组文章数量:1415139
what is the correct way to add an optional validation object in vuelidate?
Given a validation shape of:
validations: {
vehicles: {
$each: {
type: {
required
},
engine: {
required: requiredIf((vehicle) => vehicle.type == 'car'),
size: {
required
},
power: {
required
}
}
}
}
}
My expectation was that for a vehicle of type bike
I would not need to provide a size and power as their parent engine
is not required. However the validation is returning invalid.
what is the correct way to add an optional validation object in vuelidate?
Given a validation shape of:
validations: {
vehicles: {
$each: {
type: {
required
},
engine: {
required: requiredIf((vehicle) => vehicle.type == 'car'),
size: {
required
},
power: {
required
}
}
}
}
}
My expectation was that for a vehicle of type bike
I would not need to provide a size and power as their parent engine
is not required. However the validation is returning invalid.
- Can you include more of your implementation? Or at least what your model looks like? – LHM Commented Feb 17, 2020 at 18:14
2 Answers
Reset to default 3What you could do instead of asking for each subfield of engine to be required is to ask every engine to have both size
and power
, so that for everytime there is an engine it must have those keys. Add this together with the requiredIf and you have the following: Every vehicle must have a type, if it is a car then an engine is required and every engine must have a power and size
.
The following snippet shows it working.
Vue.use(vuelidate.default);
let { required, requiredIf, helpers } = validators;
const contains = (param) =>
(value) => !helpers.req(value) ||
param.reduce((accum, curr) => accum && curr in value, true);
var app = new Vue({
el: '#app',
data: () => ({
vehicles: [
{
type: 'car',
engine: {
size: 5,
power: 2.5,
}
},
{
type: 'bike',
}
]
}),
validations: {
vehicles: {
$each: {
type: { required },
engine: {
required: requiredIf((value) => value.type === 'car'),
contains: contains(['size', 'power']),
}
}
}
},
created() {
console.log(this.$v.$invalid);
}
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vuelidate.min.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/validators.min.js"></script>
<div id="app"></div>
I went through a similar situation recently and resolved using 'parentVm', as documented at https://vuelidate.js/#sub-accessing-ponent.
You should try this:
...
engine: {
required: requiredIf((parentVm) => {return parentVm.type === 'car'}),
size: {between: between(yourMinValue, yourMaxValue)},
power: {between: between(yourMinValue, yourMaxValue)},
}
...
本文标签: javascriptOptional nested validation in vuelidateStack Overflow
版权声明:本文标题:javascript - Optional nested validation in vuelidate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745218195a2648268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论