Search code examples
javascriptvue.jsvuetify.jsnuxt.jsvee-validate

vee-validate different text-field values


I use vee-avlidate 3 and Nuxt.js

How can I make a rule that checks 2 text-field values are different ?

For example the current_password field should be different from new_password field.

Password.vue

<template>
  <v-app>
    <ValidationObserver ref="form" v-slot="{ invalid, validated, passes, validate }">
      <form>
        <ValidationProvider rules="required|min:6" v-slot="{ errors, valid }">
          <v-text-field
            v-model="current_password"
            label="Current Password"
            type="password"
            :success="valid"
            :error-messages="errors"
          />
        </ValidationProvider>
        <ValidationProvider rules="required" v-slot="{ errors, valid }">
          <v-text-field
            v-model="new_password"
            label="New Password"
            type="password"
            :success="valid"
            :error-messages="errors"
          />
        </ValidationProvider>
      </form>
    </ValidationObserver>
  </v-app>
</template>

<script>
import { ValidationObserver, ValidationProvider } from "vee-validate";
export default {
  data() {
    return {
      current_password: "",
      new_password: "",
    };
  },
  components: {
    ValidationObserver,
    ValidationProvider
  },
};
</script>

Thanks


Solution

  • Have a read of the documentation for cross field validation. Essentially the steps are

    1. Wrap the fields within the same ValidationObserver component.
    2. Give each field a name property
    3. Reference the target field name or vid value in the rules of the other.

    For your code:

    <template>
      <v-app>
        <ValidationObserver ref="form" v-slot="{ invalid, validated, passes, validate }">
          <form>
            <ValidationProvider rules="required|min:6" v-slot="{ errors, valid }" name='password'>
              <v-text-field
                v-model="current_password"
                label="Current Password"
                type="password"
                :success="valid"
                :error-messages="errors"
              />
            </ValidationProvider>
            <ValidationProvider rules="required|distinct:@password" v-slot="{ errors, valid }" name='new password'>
              <v-text-field
                v-model="new_password"
                label="New Password"
                type="password"
                :success="valid"
                :error-messages="errors"
              />
            </ValidationProvider>
          </form>
        </ValidationObserver>
      </v-app>
    </template>
    
    <script>
    import { ValidationObserver, ValidationProvider, Extend } from "vee-validate";
    
    extend('distinct', {
      params: ['target'],
      validate(value, { target }) {
        return value !== target;
      },
      message: 'Fields can not be the same'
    });
    
    export default {
      data() {
        return {
          current_password: "",
          new_password: "",
        };
      },
      components: {
        ValidationObserver,
        ValidationProvider
      },
    };
    </script>