最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vuetify : Is it possible to add many rules from data on a text component with vuetify? - Stack Overflow

programmeradmin2浏览0评论

I need to define rules in a mixin for my ponents.

Here is a simple example of my request

/

The code :

<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',
],

However, according to the documentation

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

, I have the possibility of passing an array but there, I have the error:

Rules should return a string or boolean, received 'object' instead

I also tried to use the properties puted as :

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
},

But same error

Is there a way to get what I want?

Thank you

I need to define rules in a mixin for my ponents.

Here is a simple example of my request

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

The code :

<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',
],

However, according to the documentation

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

, I have the possibility of passing an array but there, I have the error:

Rules should return a string or boolean, received 'object' instead

I also tried to use the properties puted as :

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
},

But same error

Is there a way to get what I want?

Thank you

Share Improve this question asked Dec 27, 2019 at 10:21 Alexis GatuingtAlexis Gatuingt 5471 gold badge7 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

What you doing now is passing array containing 2 other arrays into rules while Vuetify expects array of functions.

You need to merge two arrays first. Easiest way to do it is using spread syntax:

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

评论列表(0)

  1. 暂无评论