如何在验证失败时阻止 Vuetify 3 的 v-combobox 添加新项?

huangapple go评论67阅读模式
英文:

How can we stop vuetify 3 v-combobox from adding new items if the validation checks fail?

问题

我已经尝试使用 validation-onrules 属性,它们能够进行验证并给我提供错误消息,但新项目仍然会附加到状态中。是否有办法改变这种行为,以便每次出现验证错误时都不将项目附加到状态中?

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>

我想要的

基本上,我不希望用户添加以数字开头或全部为数字的标签

例如 1232VueJs45689068yjkk 等等。

英文:

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

...
 &lt;MultiSelect
 	v-model=&quot;form.tags&quot;
	label=&quot;Select Tags&quot;
    :items=&quot;tags&quot;
    item-title=&quot;name&quot;
    item-value=&quot;id&quot;
  /&gt;
...

MultiselectComponent.vue

&lt;template&gt;
    &lt;v-combobox 
        multiple 
	chips 
        closable-chips 
        clearable 
        :return-object=&quot;false&quot;
        variant=&quot;outlined&quot;
    /&gt;
&lt;/template&gt;

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,然后通过筛选管道传递值:

&lt;v-combobox
  :model-value=&quot;values&quot;
  @update:model-value=&quot;values = filterInvalid($event)&quot;
  ...
/&gt;

您还可以在 :modelValue 输入上使用筛选器,以过滤任何无效的传入值,具体取决于是否有预设值以及如何处理它们是否无效。

以下是一个示例:

const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
  setup(){
    return {
      values: ref([12, &#39;12n&#39;,&#39;n&#39;]),
      filterInvalid: (inputValues) =&gt; inputValues.filter(value =&gt; typeof value === &#39;string&#39; &amp;&amp; isNaN(value[0]))
    }
  }
}
createApp(app).use(vuetify).mount(&#39;#app&#39;)
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css&quot; />
<link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;>
<div id=&quot;app&quot;>
  <v-app>
    <v-main class=&quot;pa-8&quot;>
      <v-combobox
        :model-value=&quot;filterInvalid(values)&quot;
        @update:model-value=&quot;values = filterInvalid($event)&quot;
        multiple
        chips
        closable-chips
        clearable
        variant=&quot;outlined&quot;
      ></v-combobox>
      
      <div>Values: {{values}}</div>
      
    </v-main>
  </v-app>
</div>
<script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;></script>
<script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js&quot;></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:

&lt;v-combobox
  :model-value=&quot;values&quot;
  @update:model-value=&quot;values = filterInvalid($event)&quot;
  ...
/&gt;

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, &#39;12n&#39;,&#39;n&#39;]),
      filterInvalid: (inputValues) =&gt; inputValues.filter(value =&gt; typeof value === &#39;string&#39; &amp;&amp; isNaN(value[0]))
    }
  }

}
createApp(app).use(vuetify).mount(&#39;#app&#39;)

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css&quot; /&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;div id=&quot;app&quot;&gt;
  &lt;v-app&gt;
    &lt;v-main class=&quot;pa-8&quot;&gt;
    
      &lt;v-combobox
        :model-value=&quot;filterInvalid(values)&quot;
        @update:model-value=&quot;values = filterInvalid($event)&quot;
        multiple
        chips
        closable-chips
        clearable
        variant=&quot;outlined&quot;
      &gt;&lt;/v-combobox&gt;
      
      &lt;div&gt;Values: {{values}}&lt;/div&gt;
      
      &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js&quot;&gt;&lt;/script&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年6月1日 18:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定