4

Is it possible to assign multiple rules to validate input? For example I have two rules:

alphaRule: [
      v => /[a-zA-Z]+$/.test(v) || 'Field must only contain letters'
    ],
requiredRule: [
               v => !!v || "required field"
            ]

And I have number of controls. Some of them need only requiredRules, some - only alphaRule, but some - combination of alphaRule and requiredRule.

Can I combine it together?

something like

:rules = "alphaRule, requiredRule"

3 Answers 3

8

If you want to leave both variables, you can just work with them like with JS arrays.

:rules = "alphaRule.concat(requiredRule)"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. That's exactly what I need
how about if we want to combine 3 or more rules?
@noel293, first.concat(second).concat(third), i guess :)
1

Just keep adding the rules into the array:

:rules = "[alphaRule, requiredRule].flat()"

Comments

-1

this works for me

v-bind:rules="[mr.required, mr.counter]"

and this is the rules:

enter code here

1 Comment

Your answer do not explains in detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.