我目前正在处理表单,但在满足条件时启用按钮时遇到一些问题。
目前,我创建了一个表单,其中包含用于填写姓名、电话号码、一些选项和消息的输入。当条件满足时,我想启用我已经禁用的按钮。
有人知道我可以再次启用该按钮来提交表单吗?
<template> <div class="contact"> <h1>Vi uppskattar alla frågor som du har angående denna applikation!</h1> <h2> Vänligen skriv ett meddelande här nedan så ska vi göra allt vi kan för att svara så snart som möjligt!</h2> </div> <form @submit.prevent="submitForm"> <div class="form-control" :class="{invalid: fullNameValidation === 'invalid'}"> <label for="name">Namn</label> <input id="name" name="name" type="text" v-model="fullName" @blur="validateInput"> <p v-if="fullNameValidation === 'invalid'">Vänligen fyll i ett namn</p> </div> <div class="form-control" :class="{invalid: phoneValidation === 'invalid'}"> <label for="phone">Telefonnummer</label> <input id="phone" name="phone" type="number" v-model="phoneNr" @blur="validatePhone" pattern="[0-9]*"> <p v-if="phoneValidation === 'invalid'">Vänligen fyll i ett giltigt telefonnummer</p> </div> <div class="form-control"> <label for="referrer">Hur fick du kännedom av denna applikation?</label> <select id="referrer" name="referrer" v-model="referrer"> <option value="" disabled hidden>Välj ett alternativ</option> <option value="internet">Internet</option> <option value="friends">Vänner</option> <option value="newspaper">Nyhetstidningar</option> <option value="other">Annat</option> </select> </div> <div class="form-control" :class="{invalid: messageValidation === 'invalid'}"> <label for="message">Meddelande</label> <textarea name="message" id="message" cols="30" rows="10" v-model="message" @blur="validateMessage"></textarea> <p v-if="messageValidation === 'invalid'">Vänligen fyll i ditt meddelande</p> </div> <div> <button v-on:click="$router.push('thankyou')" :disabled="!isComplete" id="myBtn">Skicka meddelande</button> </div> </form> </template> <script> export default { data() { return { fullName: '', fullNameValidation: 'pending', phoneNr: 'null', phoneValidation: 'pending', referrer: '', messageValidation: 'pending' } }, methods: { submitForm() { this.fullName = ''; }, validateInput() { if (this.fullName === '') { this.fullNameValidation = 'valid' } else { this.fullNameValidation = 'invalid' } }, validatePhone() { if (this.phoneNr > 10) { this.phoneValidation = 'valid' } else { this.phoneValidation = 'invalid' } }, validateMessage() { if (this.messageValidation > 1) { this.messageValidation = 'valid' } else { this.messageValidation = 'invalid' } }, computed: { isComplete() { return Object.values(this.fields).every(({valid}) => valid) } } } } </script>
P粉2783794952024-02-22 11:55:36
将表单字段与错误对象一起合并到一个模型中。
然后使用 Object.keys
获取已知字段键进行验证。
export default { data() { return { form: { errors: {}, values: { fullName: '', phoneNr: '', referrer: '', } } } }, methods: { validate(field) { let fields = [] // single field if (field) { delete this.form.errors[field] fields.push(field) } else { this.form.errors = {} // all fields fields = Object.keys(this.form.values) } if (fields.includes('fullName')) { if (this.form.values.fullName === '') { this.form.errors.fullName = 'Enter your full name' } else if (this.fullName !== some other validation) { ... } } if (fields.includes('phoneNr')) { if (this.form.values.phoneNr === '') { this.form.errors.phoneNr = 'Enter your phone number' } } if (fields.includes('referrer')) { if (this.form.values.referrer === '') { this.form.errors.referrer = 'Enter referrer' } } // if errors is empty return true return !Object.keys(this.form.errors).length }, submit() { // validate all if (this.validate()) { // do some thing, form is valid, if native form handler, return true/false } } } }
{{ form.errors.fullName }}
如果您不是特别需要 @blur
验证,它会大大简化事情,并且在提交时进行验证而不是在模糊各个字段时进行验证是相当标准的。
export default { data() { return { form: { errors: {}, values: { fullName: '', phoneNr: '', referrer: '', } } } }, methods: { validate() { this.form.errors = {} if (this.form.values.fullName === '') { this.form.errors.fullName = 'Enter your full name' } else if (this.form.values.fullName !== some other validation) { ... } if (this.form.values.phoneNr === '') { this.form.errors.phoneNr = 'Enter your phone number' } if (this.form.values.referrer === '') { this.form.errors.referrer = 'Enter referrer' } // if errors is empty return true return !Object.keys(this.form.errors).length }, submit() { if (this.validate()) { // do something, form is valid, if native form handler, return true/false } } } }
{{ form.errors.fullName }}