0
votes

I'm using Vee validate for from validation, validation rules are coming from backend and passed to component data in following format:

"rules": {
    "password": "{ required: true, regex: /(?=.*\\d)(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}/ }",
    "old_password": "required"
 }

How can i use rule from data in v-validate directive?

<input name="password" ref="password" v-model="password" v-validate="rules.password">

this syntax give me an error No such validator ''{ required' exists. Looks like vue is parsing the v-validate value somehow

p.s. for validation rules which are simple strings it works ok

 <input name="old_password" v-model="old_password" v-validate="rules.old_password">
1
did you try v-validate="rules.password.required" ?Boussadjra Brahim
it will not work, i need to pass whole string { required: true, regex: /(?=.*\\d)(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}/ } as validation ruleMaxim
JSON.parse()?ssb
I believe the problem is that the value of password is a String instead of an ObjectGiovane
@Giovane you are right, if the value is object it works as expected. but I don't really understand why :( My assumption is that rules.password are not really inserted in html, but processed as js variableMaxim

1 Answers

0
votes

changing "password" to Object solved the issue. Thanx Giovane for his comment.

"rules": {
    "password": { "required": true, "regex": "/(?=.*\\d)(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}/ }",
    "old_password": "required"
}