【发布时间】:2020-01-24 15:12:13
【问题描述】:
电话有一个前缀字段,电话本身有一个字段。验证必须是动态的。最小长度由公式 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),
},
},
【问题讨论】:
标签: javascript validation vue.js dynamic logic