How get all validator message?

How get all messages when use API ? Adonis always return view welcome, when validator fails. :frowning: . I need when validator fails, it will return errors or i can get all messages and render at view :frowning:

1 Like

Your question is broad, but have you done any research? For example reading the documentation covers the basics of this subject.

You can try to make custom validator, something like this :

"use strict";

class AddProduct {
	get rules() {
		return {
			product_identifier: "required|min:3"
		};
	}
	get messages() {
		return {
			"product_identifier.required": "Product identifier is required",
			"product_identifier.min": "Product identifier must be 3 characters long"
		};
	}
	async fails(errorMessages) {
		return this.ctx.response.json({ error: errorMessages[0].message });
	}
}

module.exports = AddProduct;

Save it on app/Validators/AddProduct.js and attach it on your route like :

Route.post("/product", "ProductController.postAdd").validator("AddProduct")
1 Like

Tks you, another question how make “Function Fails” global ? apply for all validators fails ?

You mean to make GlobalMiddleware to check request input?

No. i want it apply for all validators, i want change response of all validators, Example above, you write in class “AddProduct”, i don’t want it only in AddProduct. Or I need parent class custom ? uhm… Ok, tks you.

class ParentValidators {
	async fails(errorMessages) {
		return this.ctx.response.json({ error: errorMessages[0].message });
	}
}

class AddProduct extends ParentValidators {
	get rules() {
		return {
			product_identifier: "required|min:3"
		};
	}
	get messages() {
		return {
			"product_identifier.required": "Product identifier is required",
			"product_identifier.min": "Product identifier must be 3 characters long"
		};
	}
}
class OtherValidator extends ParentValidators {
}

Does this help

 get validateAll () {
    return true
  }

https://adonisjs.com/docs/4.1/validator#_validate_all