Vue Answers

How to access data in form rule with Vue Vuetify?

Spread the love

In Vuetify, we can access data in form rules by using the v-model directive to bind form inputs to data properties and then referencing those properties in your form rules.

For example, we write

  <v-form @submit.prevent="submitForm">
    <v-text-field v-model="username" label="Username" required></v-text-field>
    <v-text-field v-model="password" label="Password" required></v-text-field>
    <v-btn type="submit" :disabled="!validForm">Submit</v-btn>

export default {
  data() {
    return {
      username: '',
      password: ''
  computed: {
    validForm() {
      return this.username !== '' && this.password !== '';
  methods: {
    submitForm() {
      // Form submission logic

In this example we have two text fields for username and password, each bound to username and password data properties using v-model.

We use the required attribute in the text fields to ensure they are not empty.

We have a computed property called validForm which returns true if both the username and password fields have values, and false otherwise.

The “Submit” button is disabled when the validForm computed property returns false, preventing form submission until both fields are filled.

The form submission logic is handled by the submitForm method, which we can implement as needed.

We can add more complex form rules by using additional computed properties or methods that reference other data properties or perform custom validation logic based on our requirements.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *