I'm currently working on a form and I'm having some issues enabling the button when a condition is met.
Currently I have created a form with inputs for name, phone number, some options and message. When the condition is met, I want to enable the button that I have disabled.
Does anyone know how I can enable the button again to submit the form?
<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>
Merge form fields into one model together with error objects.
Then use
Object.keys
to obtain known field keys for verification.If you don't specifically need
@blur
validation, it simplifies things a lot, and it's pretty standard to validate on submit rather than when blurring individual fields.