VeeValidate with API Response

When you post data from Vue app to the server via Rest Api, server will validate the request. If validation fails server sends the response to app with error messages.

So how do you show these error messages in your Vue app?

For example, you have entered invalid email and response will be like this

VeeValidate Json Response

So once you get the response you have to show the error message below the respective form element

VeeValidate has got two ways to do this.

  • Use the setErrors() of the ValidationObserver
  • Use the setErrors() of the ValidationProvider

Since the response comes with states code 422, you can access the error object inside the catch() statement of the axios promise

This pass the error object to the ValidationObserver named observer. You see the following code of your Vue component. Here I have wrapped email fileld with ValidationProvider and ValidationObserver

By looking at the following diagram you can understand how the response errors are displayed in your Vue code
ValidationObserver

If you want to manually update only single field you can use the following code