Search code examples
vuejs2vuelidate

is there a vuelidate notSameAs validator


I came accross this issue about a simple password change form, in which I want to make sure that the new password is different from the old

I've tried implementing it directly following the documentation :

import { ref, withParams } from 'vuelidate/lib/validators'
export const differsFrom = equalTo => withParams(
  {type: 'differsFrom', eq: equalTo},
  function (value, parentVm) {
    return value !== ref(equalTo, this, parentVm)
  }
)

Now, importing the proper items from vuelidate is not as easy as the documentation states it.


Solution

  • Use not:

    import { sameAs, not } from 'vuelidate/lib/validators'
    
    export default {
      data () {
        return {
          password: '',
          oldPassword: ''
        }
      },
      validations: {
        password: {
          not(sameAs('oldPassword'))
        }
      }
    }