英文:
How can we stop vuetify 3 v-combobox from adding new items if the validation checks fail?
问题
我已经尝试使用 validation-on
和 rules
属性,它们能够进行验证并给我提供错误消息,但新项目仍然会附加到状态中。是否有办法改变这种行为,以便每次出现验证错误时都不将项目附加到状态中?
ParentComponent.vue
...
<MultiSelect
v-model="form.tags"
label="选择标签"
:items="tags"
item-title="name"
item-value="id"
/>
...
MultiselectComponent.vue
<template>
<v-combobox
multiple
chips
closable-chips
clearable
:return-object="false"
variant="outlined"
/>
</template>
我想要的
基本上,我不希望用户添加以数字开头或全部为数字的标签
例如
123
,2VueJs
,456890
,68yjkk
等等。
英文:
I have tried using the validation-on
, rules
props, they are able to validate and give me error messages but the new items are still getting appended to the state. Is there any way to change this behaviour so that every time there is a validation error it don't append the item to the state?
ParentComponent.vue
...
<MultiSelect
v-model="form.tags"
label="Select Tags"
:items="tags"
item-title="name"
item-value="id"
/>
...
MultiselectComponent.vue
<template>
<v-combobox
multiple
chips
closable-chips
clearable
:return-object="false"
variant="outlined"
/>
</template>
What I want
Basically I don't want user to add tags that starts with a number or are all numbers
> e.g. 123
, 2VueJs
, 456890
, 68yjkk
etc.
答案1
得分: 0
验证用于向用户显示错误消息并阻止表单提交。但是,如果立即删除无效值,就不会显示错误消息,而值始终有效。
因此,您可以不使用验证,只需筛选组件中的值。只需将 v-model
替换为底层的 :modelValue
和 @update:modelValue
,然后通过筛选管道传递值:
<v-combobox
:model-value="values"
@update:model-value="values = filterInvalid($event)"
...
/>
您还可以在 :modelValue
输入上使用筛选器,以过滤任何无效的传入值,具体取决于是否有预设值以及如何处理它们是否无效。
以下是一个示例:
const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
setup(){
return {
values: ref([12, '12n','n']),
filterInvalid: (inputValues) => inputValues.filter(value => typeof value === 'string' && isNaN(value[0]))
}
}
}
createApp(app).use(vuetify).mount('#app')
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css" />
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-main class="pa-8">
<v-combobox
:model-value="filterInvalid(values)"
@update:model-value="values = filterInvalid($event)"
multiple
chips
closable-chips
clearable
variant="outlined"
></v-combobox>
<div>Values: {{values}}</div>
</v-main>
</v-app>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js"></script>
但要注意,自动删除值可能会让用户感觉像是错误,所以最终您可能还是更喜欢验证的方法。
英文:
Validation is used to show error messages to the user and prevent the form from being submitted. But if you remove invalid values immediately, there is no error message and the values are always going to be valid.
So instead of validation, you can just filter the values coming out of the component. Just replace the v-model
with the underlying :modelValue
and @update:modelValue
and pipe the values through a filter:
<v-combobox
:model-value="values"
@update:model-value="values = filterInvalid($event)"
...
/>
You can also use the filter on the input of :modelValue
to filter any invalid values coming in, depending on if there are preset values and how to deal with them if they are invalid.
Here it is in a snippet:
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
setup(){
return {
values: ref([12, '12n','n']),
filterInvalid: (inputValues) => inputValues.filter(value => typeof value === 'string' && isNaN(value[0]))
}
}
}
createApp(app).use(vuetify).mount('#app')
<!-- language: lang-html -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css" />
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-main class="pa-8">
<v-combobox
:model-value="filterInvalid(values)"
@update:model-value="values = filterInvalid($event)"
multiple
chips
closable-chips
clearable
variant="outlined"
></v-combobox>
<div>Values: {{values}}</div>
</v-main>
</v-app>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js"></script>
<!-- end snippet -->
Note however that removing values automatically can feel like a bug to users. You might be better off with the validation approach after all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论