Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add password matching validation in vuetify?

I am trying to create a profile form which have two fields password and rePassword. basically, Both of them should be the same.

I tried using different codes found on the web and different approaches. Some of them worked but. It did'nt actually fit in with the code.

Here is the piece of code:

Profile.vue:

<v-layout>
        <v-flex xs12 sm6>
          <v-text-field                        
            v-model="password"
            :append-icon="show ? 'visibility' : 'visibility_off'"
            :rules="[rules.required, rules.min]"
            :type="show ? 'text' : 'password'"
            name="password"
            label="Enter Password"
            hint="At least 8 characters"
            counter
            @click:append="show = !show"
          ></v-text-field>
        </v-flex>

          <v-flex xs12 sm6>
            <v-text-field            
              v-model="rePassword"
              :append-icon="show1 ? 'visibility' : 'visibility_off'"
              :rules="[rules.required, rules.min]"
              :type="show1 ? 'text' : 'password'"
              name="input-10-1"
              label="Re-enter Password"
              hint="At least 8 characters"
              counter
              @click:append="show1 = !show1"
            ></v-text-field>
          </v-flex>
          </v-layout>

This is how the script looks like:

Profile.vue (script):

data() {
      return {
        show: false,
        show1: false,
        password: 'Password',
        rePassword: 'Password',
        rules: {
          required: value => !!value || 'Required.',
          min: v => v.length >= 8 || 'Min 8 characters',
          emailMatch: () => ('The email and password you entered don\'t match')
        },
        emailRules: [
          v => !!v || 'E-mail is required',
          v => /.+@.+/.test(v) || 'E-mail must be valid'
        ],
        date: new Date().toISOString().substr(0, 10),
        menu: false,
        items: ['male', 'female'],
        address: '',
        title: "Image Upload",
        dialog: false,
        imageName: '',
        imageUrl: '',
        imageFile: ''
      }
    },
    methods: {
      pickFile() {
        this.$refs.image.click()
      },            
          onFilePicked(e) {
        const files = e.target.files
        if(files[0] !== undefined) {
          this.imageName = files[0].name
          if(this.imageName.lastIndexOf('.') <= 0) {
            return
          }
          const fr = new FileReader ()
          fr.readAsDataURL(files[0])
          fr.addEventListener('load', () => {
            this.imageUrl = fr.result
            this.imageFile = files[0] // this is an image file that can be sent to server...
          })
        } else {
          this.imageName = ''
          this.imageFile = ''
          this.imageUrl = ''
        }
        },
    }
      ,
      validate() {
        if (this.$refs.form.validate()) {
          this.snackbar = true
        }
      },
      reset() {
        this.$refs.form.reset()
      }

How do I add a password matching feature in the validation using vuetify. Thanks

like image 501
abhigyan nayak Avatar asked Jun 18 '19 06:06

abhigyan nayak


People also ask

How do I add validation to Vuetify?

To add Vuetify, navigate to the project folder cd vuetify-form-validation. When the Vuetify command starts running, it'll again ask to select which preset you want. Select the default preset.

What is lazy validation Vuetify?

lazy-validation. boolean. false. If enabled, value will always be true unless there are visible validation errors. You can still call validate() to manually trigger validation.

What is ref in Vuetify?

Validation with submit & clear A ref allows us to access internal methods on a component, for example, <v-form ref="form"> . this.$refs.form.validate() will validate all inputs and return if they are all valid or not. this.$refs.form.reset() will clear all inputs and reset their validation errors.


4 Answers

You can define custom rules:

computed: {
    passwordConfirmationRule() {
      return () => (this.password === this.rePassword) || 'Password must match'
    }
}

and use it

 <v-flex xs12 sm6>
    <v-text-field            
      v-model="rePassword"
      :append-icon="show1 ? 'visibility' : 'visibility_off'"
      :rules="[rules.required, rules.min, passwordConfirmationRule]"
      :type="show1 ? 'text' : 'password'"
      name="input-10-1"
      label="Re-enter Password"
      hint="At least 8 characters"
      counter
      @click:append="show1 = !show1"
    />
  </v-flex>
like image 176
ittus Avatar answered Oct 21 '22 23:10

ittus


very easiest way is using v-model (password and confirm_password), no need to use computation

Rule

:rules="[v => !!v || 'field is required']"

Or

:rules="[(password!="") || 'field is required']"

in password

<v-text-field label="Password*" v-model="password" type="password" required   :rules="[v => !!v || 'field is required']"></v-text-field>
         
       

confirm password field Rule

 :rules="[(password === confirm_password) || 'Password must match']"

code:

 <v-text-field label="Confirm Password*" v-model="confirm_password" type="password"  required   :rules="[(password === confirm_password) || 'Password must match']"></v-text-field>
         


           
like image 24
ßãlãjî Avatar answered Oct 21 '22 23:10

ßãlãjî


VeeValidate is great for form validation but in my opinion is overkill for resolving this question when it can be achieved in Vuetify alone.

Following on from @ittus answer, you need to remove the arrow function in passwordConfirmationRule to access this:

      return this.password === this.rePassword || "Password must match";

See this codesandbox working example (also now using Vuetify 2.x)

like image 20
Frank Furter Avatar answered Oct 22 '22 00:10

Frank Furter


Very simply using Vee-validate:

<div id="app">
  <v-app id="inspire">
    <form>
      <v-text-field
        ref="password"
        type="password"
        v-model="pass"
        v-validate="'required'"
        :error-messages="errors.collect('pass')"
        label="Pass"
        data-vv-name="pass"
        required
      ></v-text-field>
      <v-text-field
        v-model="pass2"
        type="password"
        v-validate="'required|confirmed:password'"
        :error-messages="errors.collect('pass2')"
        label="Pass 2"
        data-vv-name="pass"
        required
      ></v-text-field>

      <v-btn @click="submit">submit</v-btn>
      <v-btn @click="clear">clear</v-btn>
    </form>
  </v-app>
</div>
Vue.use(VeeValidate)

new Vue({
  el: '#app',
  $_veeValidate: {
    validator: 'new'
  },

  data: () => ({
    pass: '',
    pass2: "",
  }),
  methods: {
    submit () {
      this.$validator.validateAll()
        .then(result => {
          console.log(result)
        })
    },
    clear () {
      this.pass = ''
      this.pass2 = ''
    }
  }
})

Remember to install vee-validate first and restart your local-server.

link to codepen

link to docs

like image 22
Adam Orłowski Avatar answered Oct 21 '22 23:10

Adam Orłowski