0

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">
Maxim
  • 445
  • 2
  • 11
  • did you try `v-validate="rules.password.required"` ? – Boussadjra Brahim Oct 12 '18 at 13:53
  • it will not work, i need to pass whole string `{ required: true, regex: /(?=.*\\d)(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}/ }` as validation rule – Maxim Oct 12 '18 at 13:59
  • 1
    `JSON.parse()`? – ssb Oct 12 '18 at 14:44
  • 1
    I believe the problem is that the value of `password` is a String instead of an Object – Giovane Oct 14 '18 at 02:39
  • @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 variable – Maxim Oct 15 '18 at 06:27
  • Yeah, it should, but as you are passing it's value as a String, the js won't guess that this string is actually an object – Giovane Oct 15 '18 at 12:39

1 Answers1

0

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"
}
Maxim
  • 445
  • 2
  • 11