Vue中的动态最小长度和最大长度

时间:2020-01-24 15:12:13

标签: javascript validation vue.js dynamic logic

电话有一个前缀字段,电话本身有一个字段。验证必须是动态的。最小长度由公式7-phoneprefix.length计算得出,最大长度乘以15-phoneprefix.length。当我更改phonePrefix时,它并不是动态地实现的。我尝试不返回该对象,但该方法仍然无济于事。如果有人能帮助我,我将非常感激。 这是我的代码:

    <template v-if="currentStep === 'phone'">
        <div class="q8-form-row-text">
            <span class="second" v-t="{path:'requirement_phone'}"></span>
        </div>
        <div class="q8-form-row q8-form-row_phone-group">

            <CustomSelectComponent
                    v-if="country"
                    @country="onPhonePrefixChange"
                    :options="countries"

                    v-model="country"
                    class="phonePrefix"
            ></CustomSelectComponent>

            <input v-model="phone" class="q8-form__input q8-form__input-phone" :placeholder="$t('phone')"
                   name="phone" required="required" pattern="[0-9٠-٩]+" :minlength="getPhoneLengthValidation('min')" type="tel"
                   :maxlength="getPhoneLengthValidation('max')" ref="phone"  @blur="$v.phone.$touch()"/>
            <span v-if="$v.phone.$error" class="q8-form__input-error" v-t="{path: 'error.phoneNumberError'}"/>

        </div>
        <div class="q8-form-row">
            <button type="submit" value="submit" class="q8-form__submit q8-form__submit__yellow q8-form__submit__yellow-gradient"
                    :class="{
                    'q8-form__submit_next': submitButtonType === 'next'
                }"
                    v-t="{path: submitButtonType}" @click="handleSubmitButtonClick()" :disabled="isSubmitButtonDisabled()"></button>
        </div>

    </template>

逻辑:

  public getPhoneLengthValidation(validationOption: 'min' | 'max'): number {
        const size = validationOption === 'max' ? 15 : 7;

        return size - this.phonePrefix.length;
    }

Vue验证:

validations: {

    fullName: {
        required,
        minLength: minLength(2),
    },
    email: {
        required,
        email,
        minLength: minLength(this.minValue),
    },
    phone: {
        required,
        numeric,
    },
    PrivacyPolicy: {
        checked: ( (value) => value === true),
    },
},

1 个答案:

答案 0 :(得分:0)

请检查我的示例以了解此类实现。主要问题在于

<template>
    <div>
        <div class="form-group" :class="{ 'form-group--error': $v.fullName.$error }">
            <label class="form__label">Name</label>
            <input class="form__input" v-model="fullName" @input="$v.fullName.$touch()" />
        </div>
        <span v-if="$v.fullName.nameValidator">
            {{$v.fullName | json}}}
        </span>

        <button @click="setMinLength()" >go</button>

        <span >
            {{$v | json}}}
        </span>
    </div>
</template>


<script lang="ts">
    import {Component, Prop, Vue, Watch} from 'vue-property-decorator';
import { required, email, numeric, minLength, maxLength} from 'vuelidate/lib/validators';

Component.registerHooks(['validations']);
@Component()
export default class RegistrationFormComponent extends Vue {  

    validations() {
        return {
            fullName: {
                required,
                nameValidator: this.nameValidator
            }
        }
    }



    public fullName= '';
    public minLength:number= 3;

    nameValidator(value:string){
        return   this.minLength > value.length
    }


    setMinLength(){
        this.minLength = 5;
    }

    public created() {
    }


}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
    @import '../../assets/styles/styles.scss';
</style>
相关问题