VeeValidate
How to use Vue with VeeValidate - free starter
This article will teach you how to use VeeValidate with your MDB project. We are integrating VeeValidate functionality with MDB Vue validation properties for inputs.
Lets see how to use VeeValidate with MDB 5 Vue inputs.
Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new project
First, we need to create a new Vite project.
Step 1
Init the project and choose vue framework. You can add a name for your project as we did in example below
Step 2
Navigate to app's directory.
Step 3
Install dependencies.
Step 4
Setup MDB.
Step 5
Import CSS. Add the following line at the beginning of your main.ts file:
Step 6
Import Font Awesome and Roboto font. Add the following lines in public/index.html file inside
head
section:
Step 7
Enable sourcemaps during development.
Step 8
Launch the application.
Installation
The next thing we need to do is to install VeeValidate package.
Step 1
Navigate to your project and in your terminal run:
Step 2
Clean your project, remove HelloWorld.vue
and style.css
(don't forget to remove it from main.ts).
Step 3
We will have 8 fields for validation: first name, last name, address, country, email, phone, additional information and confirmation checkbox. Let's prepare a few validation rules that these fields must fulfill. Create validationRules.ts
inside src
directory and paste the code below.
As you can see, we have prepared rules for each field separately. For example, we have provided an expression that will check if provided e-mail address is valid. If this is not a case, we will receive a message that "This field must be a valid email".
Step 4
Now we can start working on our form. You can follow our example by coping and pasting the code below to the App.vue
file (or any other you created).
This is the form we are going to validate. MDBInput
can receive properties which will show if the field is validated correctly or not. To read more about this, visit our MDB Vue validation page where we explain how it works.
Step 5
The last thing we have to do is to connect VeeValidation
with the one we have in mdb vue
package. We need to import two things, useForm and Field
.
As you can see, a few things changed from the code before. We have imported useForm
which would give us access to two methods. handleSubmit
is responsible for submiting data. You can provide a callback as the first argument with code that will be called if each validation field passes, and another callback as the second argument with the function that will be called after validation failed.
The next thing is to wrap our inputs inside a Field
component. This will allow us to inject rules that inputs must follow to pass the validation. We can pass data from v-slot
to our MDBInput
component properties to see information if inputs are validated correctly.
That's it! Everything should work properly after clicking the submit button. If validation was unsuccessful, you should see information under each field that failed. After successful validation, you will receive an alert with data from your form, and after closing the alert resetForm
method will clear the form.
More about VeeValidate
For more information, see VeeValidate page. There you can read about other options, best practices and testing.
Frontend features
MDB UI KIT:
To create the project we used our ui kit, with which we can build basic views very quickly.
Views and Layouts:
In this project we used HelloWorld.vue
file, created by vite tool in which we placed our vue
code. We have successfully integrated i18n into the MDB package and can use the appropriate translations based on provided json
files.