I'm new to Node.js and I had a problem in customize validation message in node.js.I created a simple app with Compound JS using CRUD operation. In my app there is a field called "id". The "id" field only accept integer value only. Then I validate the field by using the following code in model/user.js.
module.exports = function (compound, User) {
var num = /^\s*\d+\s*$/;
User.validatesFormatOf('id', {with: num, message:"is not a number"});
};
By using the above code it work fine. But I also want to check whether the field is blank or not. Then I change the code a little bit. The modified code is look like this:
module.exports = function (compound, User) {
var num = /^\s*\d+\s*$/;
User.validatesFormatOf('id', {with: num, message: {blank: "can't be blank", with: "is not a number"}});
};
Then the validation message if the field is blank will be displayed as this "Id can't be blank". But when I enter the value in the id field other than numbers the validation error message will be "Id [object Object]". I think the keyword with is not supported. Is here any other keyword like "blank" or "min" to use so I can get the validation message as "Id is not a number".
I found a solution for this so the modified version of user.js is like this:
module.exports = function (compound, User) {
var num = /^\s*\d+\s*$/;
User.validatesPresenceOf('id', {message: "can't be blank"});
User.validatesFormatOf('id', {with: num, message:"is not a number"});
};
The problem with the above code is that it is displaying two validation message at the same time i.e. the default and custom blank message is displaying at the same time. My requirement is to show only one validation message for a field at a time. Is this possible?