使用vee-validate验证特定数量的输入字段

时间:2019-04-16 07:19:54

标签: vuejs2 vee-validate

当我单击下一步按钮时,我想在向导中验证varios步骤,而不验证所有输入。单击下一个按钮时,应触发该功能以验证第一步的输入字段。然后在下一步中输入第二步的输入字段,依此类推。下一个按钮是没有提交按钮。

 <tab-content title="Company" icon="fas fa-building" :before-change="validateThirdStep">
            <div class="col-md-8 offset-md-2">
                <label class="col-form-label text-md-right">Do you have a chicken?</label>
                <div class="form-goup row">
                    <div class="col-md-7">
                        <input type="radio" name="dsb" id="radios" value="yes" v-model="pickeddsb">Yes
                        <input type="radio" name="dsb" id="radios" value="no" v-model="pickeddsb">No
                    </div>
                </div>
            </div>

            <div class="form-group" v-if="pickeddsb=='yes'">
                <div class="col-md-8 offset-md-2">
                    <h4>Data</h4>
                </div>

                <div class="col-lg-8 m-auto">

                        <!-- Name -->
                        <div class="form-group row">
                        <label class="col-md-3 col-form-label text-md-right">{{ $t('name') }}</label>
                        <div class="col-md-7">
                            <input
                            v-model="namedsb"
                            v-validate="'required|namedsb'"
                            :class="{ 'has-error': errors.has('namedsb') }"
                            type="text"
                            name="namedsb"
                            >
                            {{ errors.first('namedsb') }}
                        </div>
                        </div>

                        <!-- Firm -->
                        <div class="form-group row">
                        <label class="col-md-3 col-form-label text-md-right">{{ $t('companyname') }}</label>
                        <div class="col-md-7">
                            <input
                            v-model="firm"
                            v-validate="'required|firmdsb'"
                            :class="{ 'has-error': errors.has('firmdsb') }"
                            class="form-control"
                            type="text"
                            name="firmdsb"
                            >
                            {{ errors.first('firmdsb') }}
                        </div>
                        </div>

                        <!--Telephone-->
                        <div class="form-group row">
                        <label class="col-md-3 col-form-label text-md-right">{{$t('telephone')}}</label>
                        <div class="col-md-7">
                            <input
                            v-model="telephonedsb"
                            v-validate="'required|telephonedsb'"
                            :class="{ 'has-error': errors.has('telephonedsb')}"
                            class="form-control"
                            type="tel"
                            name="telephonedsb"
                            >
                            {{ errors.first('telephonedsb') }}
                        </div>
                        </div>

                        <!-- Email -->
                        <div class="form-group row">
                        <label class="col-md-3 col-form-label text-md-right">{{ $t('email') }}</label>
                        <div class="col-md-7">
                            <input
                            v-model="emaildsb"
                            v-validate="'required|emaildsb'"
                            :class="{ 'has-error': errors.has('emaildsb') }"
                            class="form-control"
                            type="email"
                            name="emaildsb"
                            >
                            {{ errors.first('emaildsb') }}
                        </div>
                        </div>
                </div>
            </div>

        </tab-content>
export default {
data() {
return {
 namedsb: "",
 telephonedsb: "",
 emaildsb: "",
 namere: "",
 telephonere:"",
 emailre: "",
        }
 },
methods: {
   validateThirdStep: function() {
            this.$validator.validate('namedsb', this.namedsb);
            this.$validator.validate('firmdsb', this.firmdsb);
            this.$validator.validate('telephonedsb', this.state);
            this.$validator.validate('emaildsb', this.emaildsb);
        }
}
}



1 个答案:

答案 0 :(得分:0)

使用VeeValidate的内置作用域非常容易,您可以在以下页面上进行了解:enter link description here

一个简单的解释是通过在字段上添加每个标签/步骤的特定范围:

data-vv-scope="step1"

然后在按下按钮进行验证时使用此方法:

methods: {
    validateForm(scope) {
        this.$validator.validateAll('step1').then((result) => {
            if (result) {
                alert('Form Submitted!');
            }
        });
    }
}