gpt4 book ai didi

javascript - 验证 : Is it possible to add many rules from data on a text component with vuetify?

转载 作者:行者123 更新时间:2023-11-30 19:01:10 24 4
gpt4 key购买 nike

我需要在 mixin 中为我的组件定义规则。

这是我的请求的一个简单例子

https://jsfiddle.net/alexisgt01/0tg4ovnz/2/

代码:

<v-text-field :rules="[nbRules, requiredRules]" outlined v-model="name" label="Nom du ticket" required></v-text-field>

...

requiredRules: [
v => !!v || 'Le champs est obligatoire',
],
nbRules: [
v => v.length <= 10 || 'Name must be less than 10 characters',
],

但是,根据文档

Accepts an array of functions that take an input value as an argument and return either true / false or a string with an error message

,我有可能传递一个数组,但我有错误:

规则应返回字符串或 bool 值,改为接收“对象”

我还尝试使用计算为的属性:

customRules(nb = 10) {
const rules = [];

if (nb) {
const rule =
v => (v || '').length <= nb ||
`A maximum of ${nb} characters is allowed`

rules.push(rule)
}
return rules
},

同样的错误

有没有办法得到我想要的东西?

谢谢

最佳答案

您现在所做的是将包含 2 个其他数组的数组传递给 rules,而 Vuetify 需要函数数组。

您需要先合并两个数组。最简单的方法是使用扩展语法:

<v-text-field :rules="[...nbRules, ...requiredRules]" outlined v-model="name" label="Nom du ticket" required></v-text-field>

关于javascript - 验证 : Is it possible to add many rules from data on a text component with vuetify?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com