I am adding custom validation to my forms and custom fields in my
Django app. I would like to be able to modify the value of a field
when triggering an error. For example, if there is an error, the form
should be redisplayed with the field value corrected by clean() and an
error message "Data has been corrected below. Click save again to
confirm if these changes are OK"

I've tried returning the modified data in cleaned_data[] like this but
it doesn't work. It displays the error correctly, but the field value
is not updated with the corrected HTML when the form is redisplayed.

    class T34AtividadeForm(ModelForm):
        def clean(self):
            # Return cleaned html
            error,html = fix_imgs(cleaned_data.get("a34_descricao"))
            if error:
                msg = u'Data has been corrected below. Click save
again to confirm if these changes are OK';
                self._errors['a34_descricao'] = ErrorList([msg])
                # This doesn't work
                cleaned_data["a34_descricao"] = html
                # This doesn't work either
                self.a34_descricao = html

        return cleaned_data

I'd also like to do the same thing with a field, but since the errors
are triggered by exception, I don't get a chance to return the
corrected value. Like the form clean() method, the error is displayed
correctly, but the value is not updated.

    class HTMLField(CharField):
        widget = HTMLTextarea

        def clean(self, value):
            value = super(HTMLField,self).clean(value)
            error,html = fix_imgs(value)
            if error:
                # This doesn't work
                self.value = html
                raise forms.ValidationError(u'Data has been corrected
below. Click save again to confirm if these changes are OK.')
            return html
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to