On Wed, 2009-02-18 at 04:28 -0800, mrts wrote: > The last unsolved model-validation design issue is the error message > protocol (my work on fields is waiting on it).
Well, it's the the last issue, but it's certainly a stumbling block. I've been spending quite a lot of time lately trying to merge model validation into trunk and, as you'll see in the 1.1. announcement, it's not really there yet. There are a number of backwards incompatibilities that have been introduced along the way and some of the API is fairly hard to use. > Let me present the > approach that looks sane to me. > > The old Field.default_error_messages dict and corresponding > logic is no longer required (and removed in my branch) as default > error messages live in core/validators now. It *is* still required. Firstly, it's a very useful way of specifying holistic error messages. Secondly, removing it would be backwards incompatible in a major way (many custom fields would no longer work properly, for example). [...] > In reality, ValidationError contains only a single error message. The > list is required for aggregating several errors from the validator > collection associated with a field. > However, as of now, the > ValidationError class tries to be both a single value and a > collection, resulting in quite a mess. Definitely. I've managed to simplify it somewhat, but it requires some conventions. Part of the problems come from trying to make model field validation and form field validation so alike in an area where it isn't necessarily so. It turns out to be easier to have exceptions.ValidationError be more dictionary-like by default, although I'm still playing with the API a bit there. There are two possibilities here. One is that if a default error message exists for the field, we only display that when a validation error occurs. Otherwise we aggregate the individual validation error messages. The other approach is to always aggregate but we have the convention that the default (field-wide) error message is descriptive, in the sense that it describes what is expected ("must be an integer between 1 and 101"), whilst the individual validator error messages are more proscriptive about the actual problem that has been found ("is not an integer"). The latter always state a problem, not a requirement, so they read well when aggregated with the description. > > This is a classic case of the Composite pattern (http:// > en.wikipedia.org/wiki/Composite_pattern ), so I propose that > * ValidationErrors remain single values > * core/validators gets an additional ValidationErrorComposite class > that derives from ValidationError and has the same interface That's pretty good analysis. I've done the same thing in the merged code I've been doing, except slightly differently. Django.core.exceptions.ValidationError is the composite collection of error messages. Django.forms.utils.ValidationError, which you guys had nuked, remains the way to make a single HTML presentation of form field errors (that had to go back anyway for backwards-compatibility reasons, since otherwise every single custom field cleaning method would potentially break, at a minimum). There's a constructor for forms.ValidationError that takes an exceptions.ValidationError to do the conversion. Presenting model field validation errors isn't something that we have a default for, since they aren't tied to HTML output or anything like that. If presenting them as HTML output, the forms.ValidationError class could be used (that's how ModelForms would do it), but, for other cases, pulling out the errors on an as-needs required When I get out from under some work stuff this week, I'm going to commit what I've been merging (it a merge of yours and Honza's git branches and a lot of editing and review changes) into a subversion branch so that more people can test it. I'm then happy to pull in and review changes from git branches if you guys want to work on it there, but we need to have this done in a more general way. We've all dropped the ball a bit -- I thought the problems that were creeping in were going to simple to fix up in review; I was wrong. It's the broken-windows problem, where one little variation from "great" leads to another and another, without realising it. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---