Any opinions on the best place (model, controller, decorator, presenter, etc.) to put validation messages that are specific to a given context? I.e., suppose you want to display a very detailed validation message with html tags, but even if the only current clients of the model validation can accept
html_safe strings, it somewhat doesn’t feel right to putting the html code in the model validator messages.
In a real project, I did the following:
- Started off with normal model validation.
- Moved the creation of the validation messages to a Draper decorator, as I needed various view (e.g., pluralize) and url helpers.
- Checked for the error situation before calling save or validate, and used the decorator’s message for the flash.
One issue I see with standard Rails validation is that there doesn’t seem to be some sort of error code along with the message. Sure, you know the attribute, but what if there’s different errors for that attribute. With an error code, the consumer of the Errors hash could use the error code. Without the error code, one could parse the resulting string, but thats seems incorrect (what if you later internationalize?).
Additionally, the documentation declares that the keys of the Errors hash should be symbols corresponding to the model’s attributes, or else
:base. So using additional keys also seems incorrect.
Suggestions? Anything better than working outside of normal validation?
Active Model Errors Provides a modified Hash that you can include in your object for handling error messages and interacting with Action View helpers.