VUE JS。验证数组的每个字符串的问题,并在每个项目上显示错误消息
我有一个VUE项目,其中我用表格收集测试答案,我想用{quirck}验证每个输入,但我没有这样做。我有以下代码,但会导致检查是否存在数组而不是检查其是否存在。 这是html模板:
<form
class="forms-test"
v-for="(testItem, index) in testItems"
:key="testItem.block"
>
<div class="TaskComponent" id="taskSection2">
<div class="task-no-pictureS2">
<h4>{{ t("task") }}: {{ t("calculate") }}</h4>
<p>
{{ testItems[index].task }}
</p>
<BaseInput
class="answerBox"
:placeholder="t('type')"
v-model="studentAnswers[index]"
:label="t('answer')"
type="text"
/>
</div>
<p v-if="v$.studentAnswers.$error">
{{ v$.studentAnswers.$errors[0].$message }}
</p>
</div>
</form>
这是在&lt; script&gt;
data() {
return {
v$: useValidate(),
studentAnswers: [],
language: i18n.global.locale.value,
answerOptions: [
{ label: this.t("true"), value: this.t("true") },
{ label: this.t("false"), value: this.t("false") },
],
};
},
validations() {
return {
studentAnswers: {
required,
},
};
},
我尝试使用$每个
helper 中尝试的部分foreach
也无法正常工作的帮手。这就是我已经尝试的:
validations() {
return {
studentAnswers: {
$each: {
required
},
},
};
},
使用foreach
助手:
validations() {
return {
studentAnswers: {
$each: helpers.forEach({
required,
}),
},
};
},
也使用element
in foreach
中,
validations() {
return {
studentAnswers: {
$each: helpers.forEach(element =>{
element: { required }
}),
},
};
},
但这给出了皮棉错误。
有其他方法可以实现这一目标,还是我在某处犯了错误?
I have a vue project in which I collect test answers with forms, I want to have validate each input with { required } but I fail to do so. I have the following code but it results in checking if there is an array rather than checking if each string of it is there or not.
This is html template:
<form
class="forms-test"
v-for="(testItem, index) in testItems"
:key="testItem.block"
>
<div class="TaskComponent" id="taskSection2">
<div class="task-no-pictureS2">
<h4>{{ t("task") }}: {{ t("calculate") }}</h4>
<p>
{{ testItems[index].task }}
</p>
<BaseInput
class="answerBox"
:placeholder="t('type')"
v-model="studentAnswers[index]"
:label="t('answer')"
type="text"
/>
</div>
<p v-if="v$.studentAnswers.$error">
{{ v$.studentAnswers.$errors[0].$message }}
</p>
</div>
</form>
This is in the <script>
part
data() {
return {
v$: useValidate(),
studentAnswers: [],
language: i18n.global.locale.value,
answerOptions: [
{ label: this.t("true"), value: this.t("true") },
{ label: this.t("false"), value: this.t("false") },
],
};
},
validations() {
return {
studentAnswers: {
required,
},
};
},
I have tried using the $each
helper but in the vuelidate docs it says they removed it and add the new forEach
helper that doesnt work as well. This is what I have already tried:
validations() {
return {
studentAnswers: {
$each: {
required
},
},
};
},
and with the forEach
helper:
validations() {
return {
studentAnswers: {
$each: helpers.forEach({
required,
}),
},
};
},
also with element
in forEach
validations() {
return {
studentAnswers: {
$each: helpers.forEach(element =>{
element: { required }
}),
},
};
},
but this gives lint error.
Is there another way to achieve this or am I making an error somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
vocêpode fiturizar da seguinte forma:
脚本
html
Você pode utilizar da seguinte forma:
SCRIPT
HTML