Support of Setting dynamic validation message from validator.
Closed this issue · 3 comments
Consider simplified code from below
model.defineValidator('emailValidator', function (value) {
return new Promise((resolve) => {
service.checkEmail(value).then((response) => {
resolve(resp.isValid); // Resolving model to valid/invalid
// Is there a way to apply this dynamic message??
// If we use applyErrors from here it's overridden with emailValidator message.
console.log(response.message);
});
});
});
// Model definition
model.defineField('email', {
type: 'String',
validate: [
{
'validator': 'presence',
'message': 'Must enter email'
},
{
'validator': 'stringEmail',
'message': 'Must be an email'
},
{
'validator': 'emailValidator',
'message': 'required' // Can this be sett to message retuned from API inside of emailValidator function??
}
]
});
@xpepermint is there any plan to add this feature in future releases?
Hey @snovakovic, unfortunately this is not possible. Yesterday my team had a discussion on this and we concluded that the proposal is against our no-magic
policy and does not follow single responsibility principle. A validator should always return boolean
and should always validate exactly one thing. If you need to validate two different cases, you need to define 2 validators. Btw, note that you can use variables within a message - maybe this can help.
Please check how ValidatableJS and HandleableJS work. Maybe you get an idea on how to resolve your problem.
What about overloading message
to act as a string or function that returns a string?
@dailyraisin that sounds reasonable. Do you have a good case for that?