Sometimes, when using form-based validation it is not always possible or
desired to re-display the form in case of validation errors (for example
when you have a search form which displays the results in another
frame), but you still want to display the validation error messages in a
user friendly way, i.e. matching each error message to a field display
label.

I came up with this little nugget as a solution:

    @expose("mypkg.templates.results")
    @validate(form=some_form)
    def showform(self, field1, field2, fieldn, tg_errors=None):

        if tg_errors:
            errors = []
            for k, v in tg_errors.items():
                try:
                    label = some_form._field_for(k).label
                    errors.append((label, v))
                except:
                    errors.append((k, v))
            return dict(results=None, errors=errors)
        else:
            ...

This seems a bit cumbersome and also uses a private method of the
widgets.FormFieldContainer class. Is there a better solution for this?
Should the validate decorator take care of this by storing the field
labels somewhere? Suggestions welcome.


Chris

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to