Cristiano Magro
10 days ago b4849707f7cd990a76061f4fa4bf71b66cae0f76
11 - Forms/xno-forms-01-starting-setup/src/components/TheForm.vue
@@ -1,8 +1,9 @@
<template>
  <form @submit.prevent="submitForm">
    <div class="form-control">
    <div class="form-control" :class="{ invalid: usernameValidity === 'invalid' }">
      <label for="user-name">Your Name</label>
      <input id="user-name" name="user-name" type="text" v-model="userName" />
      <input id="user-name" name="user-name" type="text" v-model.trim="userName" @blur="validateInput" />
      <p v-if="usernameValidity === 'invalid'">please enter a valid name</p>
    </div>
    <div class="form-control">
      <label for="age">Your Age (Years)</label>
@@ -46,6 +47,11 @@
        <label for="how-other">Other</label>
      </div>
    </div>
    <div class="form-control">
      <rating-control v-model="rating"></rating-control>
    </div>
    <div class="form-control">
      <input type="checkbox" id="confirm-terms" name="confirm-terms" v-model="confirm">
      <label for="confirm-terms">Agree to terms of use?</label>
@@ -57,7 +63,12 @@
</template>
<script>
import RatingControl from './RatingControl.vue';
export default {
  components: {
    RatingControl,
  },
  data() {
    return {
      userName: '',
@@ -66,6 +77,8 @@
      interest: [],
      how: null,
      confirm: false,
      usernameValidity: 'pending',
      rating: null,
    }
  },
  methods: {
@@ -83,15 +96,27 @@
      console.log('Checkbox: ');
      console.log(this.interest);
      console.log('Radio buttons: ');
      console.log('- Radio buttons: ');
      console.log(this.how);
      this.interest = [];
      this.how = null;
      console.log('Confirm');
      console.log('- Confirm:');
      console.log(this.confirm);
      this.confirm = false;
    }
      console.log('- Rating:');
      console.log(this.rating);
      this.rating = null;
    },
    validateInput() {
      if (this.userName === '') {
        this.usernameValidity = 'invalid';
      } else {
        this.usernameValidity = 'valid';
      }
    },
  }
}
@@ -111,6 +136,15 @@
  margin: 0.5rem 0;
}
.form-control.invalid input {
  border-color: red;
}
.form-control.invalid label {
  color: red;
}
label {
  font-weight: bold;
}