kazupon / vue-validator

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

VueValidator.mapValidation with other computed values #334

Closed heygambo closed 7 years ago

heygambo commented 7 years ago

vue & vue-validator version

2.0.3, 3.0.0-alpha.1

how can I use VueValidator.mapValidation with other computed values that I have in the component? I've tried to figure it out but I've got a little bit unsure what it does.

Is it meant to be used like this?

  computed: {
    submitDisabled () {
      return this.sending // || this.$form.invalid
    },

    valid: VueValidator.mapValidation(['$validation.validation1.valid']),

    usernameInvalid: VueValidator.mapValidation(['$validation.validation1.form.subject.invalid']),

    passwordInvalid: VueValidator.mapValidation(['$validation.validation1.form.body.invalid'])
  }
kazupon commented 7 years ago

Thank you of your feedback!!

I think you can avoid the it with the flowing:

const { extend } = Vue.util
const computed = {
  submitDisabled () {
      return this.sending // || this.$form.invalid
    }
}
extend(computed, VueValidator.mapValidation({
    valid: '$validation.validation1.valid',
    usernameInvalid: '$validation.validation1.form.subject.invalid',
    passwordInvalid: '$validation.validation1.form.body.invalid'
})

new Vue({
  computed,
  ...
})

However, This is not very useful. I'll consider the API of convenience. 😉

aplr commented 7 years ago

Hello,

maybe the object spread operator (which is a stage-3 ECMASCript proposal) will help? I'm using it with the vuex map functions and it works great.

kazupon commented 7 years ago

Yep, you can use object spread operator.

heygambo commented 7 years ago

I've just done it that way now:

computed: {
  valid () {
    return this.$validation.validation.valid
  },

  subjectInvalid () {
    return this.$validation.validation.form.subject.invalid
  },

  bodyInvalid () {
    return this.$validation.validation.form.body.invalid
  }
}

I feel like it's easier to read. I just didn't realize I could just do that back when I've asked. Thank you for helping, though!