kazupon / vue-validator

:white_check_mark: Validator component for Vue.js
MIT License
2.19k stars 431 forks source link

How to use both field params attribute and Array the same time on one field #260

Closed aflext closed 8 years ago

aflext commented 8 years ago

I have a field need to be validated for required/minleng/maxleng and a custom validator numeric, but i have already used the field params attribute, so how to use a custom numeric at the same time on the same field. Note that the custom validator numeric dose not need any parameter.

validators: {
         // `numeric` and `url` custom validator is local registration
        numeric: {
            message: 'xxx',
            check: function (val) {
                return (/^[-+]?[0-9]+$/).test(val);
            }
        },
        url: {
            message: 'xxx',
            check: function (val) {
                return (/^(http\:\/\/|https\:\/\/)(.{4,})$/).test(val);
            }
        }
    }
kazupon commented 8 years ago

Hi! 😺

Cannot use the all validators with array syntax. You need to object syntax in multiple validators.

...
<input type="text" v-validate:field1="{ required: true, minlength: { rule: 10 }, maxlength: { rule: 29 } ... }">
...

see the validator syntax

aflext commented 8 years ago

@kazupon Thanks a lot for reply! You are saying that i could use a dummy rule for custom validator like numeric with maxlength when specify the field name to field params attribute like below ?

new Vue({
  el: '#app',
validators: {
         // `numeric` and `url` custom validator is local registration
        numeric: {
            message: 'xxx',
            check: function (val) {
                return (/^[-+]?[0-9]+$/).test(val);
            }
        },
        url: {
            message: 'xxx',
            check: function (val) {
                return (/^(http\:\/\/|https\:\/\/)(.{4,})$/).test(val);
            }
        }
    },
  data: {
    fields: [{
      id: 'username',
      label: 'username',
      name: 'username',
      placeholder: 'input your username',
      validate: { required: true, maxlength: 16 , numeric: true, email: true}
    }, {
      id: 'message',
      label: 'message',
      name: 'message',
      placeholder: 'input your message',
      validate: { required: true, minlength: 8 , numeric: true, email: true}
    }]
  }
})
kazupon commented 8 years ago

If you don't use the second argment of check, you should use the dummy rule. In the above your example, you can use the dummy rule due to check function of numericandurl` are defined the static rule.