Book Image

Building Forms with Vue.js

By : Marina Mosti
Book Image

Building Forms with Vue.js

By: Marina Mosti

Overview of this book

Almost every web application and site out there handles user input in one way or another, from registration forms and log-in handling to registration and landing pages. Building Forms with Vue.js follows a step-by-step approach to help you create an efficient user interface (UI) and seamless user experience (UX) by building quick and easy-to-use forms. You’ll get off to a steady start by setting up the demo project. Next, you’ll get to grips with component composition from creating reusable form components through to implementing the custom input components. To further help you develop a convenient user input experience, the book will show you how to enhance custom inputs with v-mask. As you progress, you’ll get up to speed with using Vuelidate and Vuex to effectively integrate your forms. You’ll learn how to create forms that use global state, reactive instant user input validation and input masking, along with ensuring that they are completely schema-driven and connected to your application’s API. Every chapter builds on the concepts learned in the previous chapter, while also allowing you to skip ahead to the topics you’re most interested in. By the end of this book, you will have gained the skills you need to transform even the simplest form into a crafted user and developer experience with Vue.
Table of Contents (15 chapters)
Title Page
Dedication
Foreword

Moving validation into our custom inputs

The amazing thing about having your own custom components is that you can craft them in any way you like. For this chapter, we're going to add support for both a valid and an invalid status to our components. The main validation logic will still be held by the parent, App.vue, as it is the containing component that holds our form.

Follow these steps to add validations:

  1. First, let's add new rules for each of our inputs. Add the following to the validations property:
validations: {
form: {
first_name: { alpha, required },
last_name: { alpha },
email: { email, required },
telephone: {
validPhone: phone => phone.match(/((\(\d{3}\) ?)|(\d{3}-))?
\d{3}-\d{4}/) !== null
},
website: { url },
love: { required }
}
},
  1. Don't forget to update your import statement to bring in the new validators that we are now...