Tutorial

Extending Vuetify Form Field Validation

Published on October 26, 2018
author

Julia Ihnatova

Extending Vuetify Form Field Validation

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Let’s explore how to extend Vuetify’s input field validation to replace empty fields with default values.

Say we have a form with some fetched values, for example, a user profile form. The user wants to edit his data and then removes his login and moves to another field instead of actually creating a new login. The login field
is empty now. Things look fine with the default Vuetify validation for empty fields:

Alliator Credentials Empty credentials

Vue’s two-way binding is syncing our data with the input and we now have a blank login field and we don’t want that behavior here. Instead we want to fill them with the initial values we fetched.

Getting Started

Let’s add a simple function to solve our problem. In real projects you’ll probably make use of a store, but here I’ll use a simple data object to save the values.

We can take an example from the Vuetify website and add the user object:

<v-form>
  <v-text-field
    v-model='gator.login'
    label='Login'
    :rules='loginRules'
    required
  ></v-text-field>
  <v-text-field
    v-model='gator.email'
    :rules='emailRules'
    label='E-mail'
    required
  ></v-text-field>
</v-form> 
export default {
  data() {
    return {
      loginRules: [v => !!v || "The input is required"],
      emailRules: [
        v => !!v || "E-mail is required",
        v => /.+@.+/.test(v) || "E-mail must be valid"
      ],
      gator: {
        login: "",
        email: ""
      }
    };
  },
  mounted() {
    this.gator.login = "crocodilian";
    this.gator.email = "crocodilian@alligator.io";
  }
};

At first, create an object to save the fetched data and fill it:

export default {
  data() {
    return {
      currentGator: {
        login: null,
        email: null
      }
    };
  },
  mounted() {
    this.currentGator.login = this.gator.login;
    this.currentGator.email = this.gator.email;
  }
};

Let’s listen to the change event and check if some of the values are empty. As we have two fields, pass-in the field name as the second argument:

<v-text-field
  v-model='gator.login'
  label='Login'
  :rules='loginRules'
  @change='checkEmpty($event, "login")'
  required
></v-text-field>
<v-text-field
  v-model='gator.email'
  :rules='emailRules'
  @change='checkEmpty($event, "email")'
  label='E-mail'
  required
 ></v-text-field>

And write the simple checkEmpty method:

methods:{
  checkEmpty(value, field){
    if(!value.trim()){
      this.gator[field] = this.currentGator[field];
    }
  }
}

That’s it! Now important fields are never empty. And if you need to add one more field just call the method with the change event and pass-in the field name.

Adding a New Input

For example, this is how you could add a name to the gator and the currentGator objects:

<v-text-field
  v-model='gator.name'
  label='Name'
  :rules='loginRules'
  @change='checkEmpty($event, "name")'
  required
></v-text-field>
export default {
  data() {
    return {
      gator: {
        login: "",
        email: "",
        name: ""
      },
      currentGator: {
        login: null,
        name: null,
        email: null
      }
    };
  },
  mounted() {
    this.gator.name = "Cayman";
    this.currentGator.name = this.gator.name;
  }
};

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


About the authors
Default avatar
Julia Ihnatova

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi, I know this post is dated but still relevant. I just wanted to note that there is an undefined data property you are using in the checkEmpty() method. You are setting this.user where you should be setting this.gator

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel