Validation failed returns only first message in vuejs


#1

Hi Guyzzz,
I want to get back all the failed messages when validation failed.But my code returns only the first message.Here is my code.

Validator/AddUser.js

'use strict'

class AddUser {
  get rules() {
    // Unique Field Restriction for Editing
    const userId = this.ctx.params.id
    const rules = {
      'newUserInfo.fname': 'required',
      'newUserInfo.lname': 'required',
      'newUserInfo.username': `required|unique:users,username,id,${userId}`,
      'newUserInfo.email': `required|email|unique:users,email,id,${userId}`,
      'newUserInfo.password': 'required|min:6'
    }
    return rules
    // return {
    //   fname: 'required',
    //   lname: 'required',
    //   username: `required|unique:users,username,id,${userId}`,
    //   email: `required|email|unique:users,email,id,${userId}`,
    //   password: 'required|min:6'
    // }
  }

  /**
   * Custom Validation Messages
   */
  get messages() {
    return {
      'newUserInfo.fname.required': 'First name is missing',
      'newUserInfo.lname.required': 'Last name is missing',
      'newUserInfo.username.required': 'Username is missing',
      'newUserInfo.username.unique': 'Username name is already registered',
      'newUserInfo.email.required': 'Email is missing',
      'newUserInfo.email.email': 'Email is not valid',
      'newUserInfo.email.unique': 'Email is already registered.',
      'newUserInfo.password.required': 'You must provide a password'
    }
  }

  /**
   *
   * validation fails messages
   *
   */
  // async fails(errorMessages) {
  //this.ctx.session.withErrors(errorMessages).flashAll()
  //return this.ctx.response.redirect('back')
  //console.log(errorMessages)
  //return errorMessages
  // }
}

module.exports = AddUser

I am using vuejs as front end and sending the request using axios
My username and email fields are unique.But it only returns the username related message even though i have provided the same email
It should return two messages. Here is the response

Any help please.
Thanks


#2

Use validateAll option https://adonisjs.com/docs/4.1/validator#_validate_all


#3

Thanks boss @virk :slight_smile: