No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Vuelidate reset form after submission

    Help
    3
    6
    6373
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • zspine
      zspine last edited by

      Hi all,

      I am using vuelidate library to validate the form and trying to reset the form after submission but for some reason it always highlights the fields with required fields. I am not sure why the reset is not working… 😞

      <template>
        <q-form ref="passwordChangeForm" @submit.prevent="submit">
      
          <validation-errors :violations="violations" message="Unable to complete your request"></validation-errors>
      
          <q-input
            ref="currentPassword"
            v-model.trim="$v.credentials.currentPassword.$model"
            type="password"
            label="Current Password"
            :rules="[
              val => $v.credentials.currentPassword.required || 'Current password is required'
            ]"
            autocomplete="turn_off_1"
          />
      
          <q-input
            ref="password"
            v-model.trim="$v.credentials.password.$model"
            type="password"
            label="New Password"
            :rules="[
                  val => $v.credentials.password.required || 'New Password is required',
                  val => $v.credentials.password.strongPassword || 'Password is required to be minimum 8 chars in length and to include at least one upper case letter and one number',
                ]"
            autocomplete="turn_off_2"
          />
      
          <q-input
            ref="confirmPassword"
            v-model.trim="$v.credentials.confirmPassword.$model"
            type="password"
            label="Confirm Password"
            :rules="[
                  val => $v.credentials.confirmPassword.required || 'Confirm Password is required',
                  val => $v.credentials.confirmPassword.sameAsPassword || 'Your password and confirm password does not match'
                ]"
            autocomplete="turn_off_3"
          />
      
          <div class="text-right q-mt-md">
            <q-btn unelevated label="Save" type="submit" color="accent" :loading="isLoading" />
          </div>
      
      
        </q-form>
      </template>
      
      <script>
      import { mapGetters, mapActions } from 'vuex';
      import { required, sameAs } from 'vuelidate/lib/validators';
      import ValidationErrors from "components/form/ValidationErrors";
      export default {
        name: 'AccountPasswordChange',
        components: {
          ValidationErrors
        },
      
        props: {
          user: {
            type: Object,
            required: true
          }
        },
      
        data() {
          const credentials = this.fields();
      
          return {
            credentials: {...credentials}
          }
        },
      
        validations: {
          credentials: {
            currentPassword: {
              required
            },
            password: {
              required,
              strongPassword:(password) => {
                return password.length >= 8 &&
                  /[a-z]/.test(password) &&
                  /[A-Z]/.test(password) &&
                  /[0-9]/.test(password)
              }
            },
            confirmPassword: {
              required,
              sameAsPassword: sameAs('password')
            }
          },
        },
      
        computed: {
          ...mapGetters({
            isLoading: 'user/update/isLoading',
            violations: 'user/update/violations',
            error: 'user/update/error'
          })
        },
      
        methods: {
          ...mapActions({
            toggleLoading: 'ux/layout/toggleLoading',
            changePassword: 'user/update/changePassword',
            resetErrors: 'user/update/resetErrors'
          }),
      
          fields() {
            return {
              currentPassword: null,
              password: null,
              confirmPassword: null
            };
          },
      
          resetForm() {
            const credentials = this.fields();
            this.credentials = {...credentials}
            this.$nextTick( () => {this.$v.credentials.$reset();} );
          },
      
          async submit() {
            this.$v.$touch();
            if (this.$v.$invalid) {
              this.$q.notify({
                message: 'Please correct the highlighted fields and try again',
                color: 'negative',
              });
            } else {
              this.resetErrors();
              const response = await this.changePassword(this.credentials);
      
              if(!this.error) {
                this.$q.notify({
                  message: 'Password successfully changed',
                  color: 'positive',
                });
      
                this.resetForm();
              }
            }
            return false;
          },
        },
      }
      </script>
      
      

      Any suggestion or help greatly appreciated

      dobbel 1 Reply Last reply Reply Quote 0
      • dobbel
        dobbel @zspine last edited by

        @zspine

        I think you get better support for your question here:

        https://github.com/vuelidate/vuelidate

        1 Reply Last reply Reply Quote 1
        • zspine
          zspine last edited by

          @dobbel Thanks for the quick reply!! I guess than it’s nothing to do with quasar elements. Sure will try!!

          metalsadman 1 Reply Last reply Reply Quote 0
          • metalsadman
            metalsadman @zspine last edited by metalsadman

            @zspine you are mixing both rules, imo better go for one or the other. Check their API of vuelidate, there should be a function there that resets the states of your validation object, qform reset isn’t gonna do it since the models on your vuelidate are still dirty.

            1 Reply Last reply Reply Quote 1
            • zspine
              zspine last edited by

              @metalsadman Thank you very much for the quick response and detailed response. As far as I checked ‘this.$v.$reset();’ is the function resets the form and also I have implemented the suggested solution from this issue and also tried with the data model values nothing seems to be working 😞 I will try this in a sandbox to find out if it’s conflicting with any other plugins or functions.

              Thanks again for your detailed reply! Cheers

              1 Reply Last reply Reply Quote 0
              • zspine
                zspine last edited by zspine

                @metalsadman Thank you very much for guiding me in the right direction. Finally it worked after adding the quasar form reset… (vuelidate form reset not working as expected)

                    resetForm() {
                      const credentials = this.fields();
                      this.credentials = {...credentials};
                
                      this.$refs.passwordChangeForm.reset();
                    },
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post