Closed vladshcherbin closed 9 years ago
You shouldn't define error messages globally. As many validators as many possible error messages. You can always write your messages container or constructor. However it could be nice module that it's not integral part of validators. I will add it to my TODO list.
We could go even further. We could pass functions instead string messages and thanks to that we could compose detailed error messages in many languages using MessageFormat package.
@jagi Can't agree with you here. We absolutely need global validation messages because now we have english default messages. If we need e.g. German ones - we need to specify them directly everywhere. Not flexible.
Instead, we can define the default messages globally and use the default ones easily. Just make an object with default messages and take them from there. If there is an object with custom messages - grab them from there. This way, we can replace the messages easily.
The fieldName can be added like so:
required: "The :attribute field is required.",
date: "The :attribute is not a valid date."
I also think this should be in core validation package. And smth complex with changing message according to currently selected language, used in multilingual website, can be a separate package.
Ok, maybe it's good idea to give developer ability to hook into the process of creating error message. It would fit nicely into the modularity of Meteor Astronomy package.
I have to figure out how to fit it nicely into current API. Maybe I will need to change some namespacing. Right now there are following objects/functions:
Astronomy.Validator
function creates new validatorAstornomy.Validators
object stores all validatorsIf I would have to give a developer some function to hook into the process of composing an error message I would have to do it in the Astronomy.Validators
object and move all the validators from to global Validators
object. I would also use events system to hook into the process. It would look like follows:
Astronomy.Validator
creates new validatorValidators
stores all validators like Validators.and
, Validators.required
etc.Astronomy.Validators.on('message', function() {})
- message event allows hooking into the process of creating an error messageLet's discuss now how onMessage
event could work. It needs to now few things:
Having that we could create event handler like this:
Astronomy.Validators.on('message', function(data) {
var messages = {
required: 'The ' + data.field + ' field is required',
date: 'The ' + data.field + ' field is not valid date'
};
return messages[data.validator];
});
I think such approach would give huge flexibility.
@jagi yep, something like this would be nice
I was thinking about that and came to conclusion that we should give developer even more flexibility. Error messages during generation process will pass through few steps.
Astro.Validators.on('error')
as described abovePost.addEvent('error')
where for every model the error messages can be differentThanks to that a developer will be able to hook into the process of the message generation on three different stages.
Now I'm working on the new events system that will allow for easier events implementation. Stay tuned :-)
Ok I've made an update about which you can read here: https://github.com/jagi/meteor-astronomy-validators#creating-error-message
Ability to hook into generating error message per schema. I will also add global hook (for all models/schemas) but it will need the whole system of global events on which I will be working soon.
For now I'm closing this issue. Thanks for help. With every suggestion Astronomy becomes more flexible.
The default error messages have to be editable with a simple object / array, containing the messages, so that we can write smth like:
That way we can define our own messages in different language or just make them what we want.
It is not possible now, right? Or did I miss something?