Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate forms inside vuetify stepper using vee-validate

Tags:

I am trying to validate the form inside the stepper component of vuetify. I have managed somehow to do the validation. But i am confused whether this is the good way for doing the validation on vuetify stepper.

I am using the same number of method as the number of step in the wizard. I am using vee-validate for the validation with the scope.

  methods: {
    submitForm1(scope) {
      debugger;
      this.$validator.validateAll(scope).then(result => {
        if (result) {
          this.e1 = 2;
        }
      });
    },
    submitForm2(scope) {
      debugger;
      this.$validator.validateAll(scope).then(result => {
        if (result) {
          this.e1 = 3;
        }
      });
    }
  },

Data:

  data() {
    return {
      e1: 0,
      name: "",
      resultStep1: true
    };
  }

Vuetify Stepper: Form1

<v-stepper-content step="1">
    <form data-vv-scope="form1">
        <v-card color="lighten-1" class="mb-5" height="200px">
            <v-card-text>
                <v-text-field 
                v-model="name" label="Contract Type" 
                :counter="10" 
                :error-messages="errors.collect('name')" 
                v-validate="'required|max:10'" 
                data-vv-name="name" required data-vv-scope="form1">
            </v-text-field>
            </v-card-text>
        </v-card>
        <v-btn color="secondary" @click.native="submitForm1('form1')">Next</v-btn>
        <v-btn flat>Cancel</v-btn>
    </form>    
</v-stepper-content>

Form 2:

<v-stepper-content step="2">
    <form data-vv-scope="form2">
        <v-card color="lighten-1" class="mb-5" height="200px">
            <v-card-text>
                <v-text-field 
                v-model="email" label="Contract Email" 
                :counter="10" 
                :error-messages="errors.collect('email')" 
                v-validate="'required|max:10'" 
                data-vv-name="email" required data-vv-scope="form2">
            </v-text-field>
            </v-card-text>
        </v-card>
        <v-btn color="secondary" @click.native="submitForm2('form2')">Next</v-btn>
        <v-btn flat>Cancel</v-btn>
    </form>   
</v-stepper-content>

It seems it will be hard to manage the forms and validation if the step increases, with the way by which I am implementing. Because i need to add the number of method with the number of steps.

Also, please suggest me if i wanted to have a button for making the form back. How should i do it?

like image 310
aakash Avatar asked Apr 28 '18 10:04

aakash


1 Answers

Assuming that e1 is your v-model for stepper :

I think you can factorise your submitForm function, like this :

methods: {
  submitForm(scope) {
    debugger;
    this.$validator.validateAll(scope).then(result => {
      if (result) {
        this.e1++;
      }
    });
  },
  goBack(){
    this.e1--
  }
}

Just call it like this in your html :

@click.native="submitForm('yourFormScopeName')"

And you can bind goBack method on an other button or wathever you want

like image 171
Toodoo Avatar answered Sep 28 '22 19:09

Toodoo