Ben Gerdemann
unread,Mar 17, 2009, 11:16:13 AM3/17/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
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