Created
December 10, 2014 16:39
-
-
Save gadelkareem/8a6668e8fba7c6833ec7 to your computer and use it in GitHub Desktop.
Custom validation messages in blueprint for sails.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For adding custom messages if using blueprint | |
// In reponses/badrequest.js | |
// after | |
// if (sails.config.environment === 'production') { | |
// data = undefined; | |
// } | |
// add | |
if (data.invalidAttributes !== undefined) { | |
var attributes = data.invalidAttributes; | |
_.each(attributes, function (attribute, key) { | |
_.each(attribute, function (properties) { | |
var rule = properties.rule; | |
var message = false; | |
try { | |
message = sails.__('validation.' + key + '.' + rule, key); | |
} | |
catch (e) { | |
} | |
try { | |
message = sails.__('validation.' + rule, key); | |
} | |
catch (e) { | |
} | |
if (message && message != 'validation.' + rule && message != 'validation.' + key + '.' + rule) { | |
properties.message = message; | |
} | |
}); | |
}); | |
} | |
// In config/i18n.js | |
//add | |
objectNotation: true | |
// example : | |
//In config/locales/en.json or your locale file | |
//add | |
"validation": { | |
"isJson": "invalid JSON format in %s" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment