logaretm / vee-validate

✅ Painless Vue forms
https://vee-validate.logaretm.com/v4
MIT License
10.82k stars 1.27k forks source link

v-validate support Custom Rules #2048

Closed de1ck closed 5 years ago

de1ck commented 5 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

v-validata = 'validator '

const validator = { getMessage(field, args) { // will be added to default locale messages. // Returns a message. }, validate(value, args) { // Returns a Boolean or a Promise that resolves to a boolean. } };

Describe the solution you'd like A clear and concise description of what you want to happen.

https://baianat.github.io/vee-validate/guide/custom-rules.html#custom-rules

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

refer to element-ui form validate

Additional context Add any other context or screenshots about the feature request here.

logaretm commented 5 years ago

Sorry, but I have no clue what is the problem here. Can you fill the template and describe the problem in detail? A live example might help as well.

de1ck commented 5 years ago

@logaretm v-validata support on String and simple Object https://baianat.github.io/vee-validate/api/directive.html I hope v-validata support Custom Rules similar syntax : https://baianat.github.io/vee-validate/guide/custom-rules.html#custom-rules

examle:

  <input v-validate="rule">
  data() {
    return {
      rule: {
        getMessage(field, args) {
          // will be added to default locale messages.
          // Returns a message.
        },
        validate(value, args) {
          // Returns a Boolean or a Promise that resolves to a boolean.
        }
      }
    }
  }
logaretm commented 5 years ago

@de1ck This won't work, you should use Validator.extend to make sure your rule can be used, you should read the following sections here

So you need to do this:

// anywhere in your code
Validator.extend('rule', {
        getMessage(field, args) {
          // will be added to default locale messages.
          // Returns a message.
        },
        validate(value, args) {
          // Returns a Boolean or a Promise that resolves to a boolean.
        }
});

Then you can use it like this:

<input v-validate="'rule'">
batuhantozun commented 5 years ago

@de1ck This won't work, you should use Validator.extend to make sure your rule can be used, you should read the following sections here

So you need to do this:

// anywhere in your code
Validator.extend('rule', {
        getMessage(field, args) {
          // will be added to default locale messages.
          // Returns a message.
        },
        validate(value, args) {
          // Returns a Boolean or a Promise that resolves to a boolean.
        }
});

Then you can use it like this:

<input v-validate="'rule'">

If I do this in my code, its throws an error at console like No such validator 'RULE' exists.

logaretm commented 5 years ago

Rules are case sensitive, the error message you posted suggests you used an uppercase rule name.