Modify value of a Django form field during clean()

2,988 views
Skip to first unread message

Ben Gerdemann

unread,
Mar 17, 2009, 11:16:13 AM3/17/09
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

Karen Tracey

unread,
Mar 18, 2009, 9:19:52 AM3/18/09
to django...@googlegroups.com
On Tue, Mar 17, 2009 at 11:16 AM, Ben Gerdemann <ger...@gmail.com> wrote:

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

Modifying cleaned_data doesn't work because when validation fails the whole cleaned_data dictionary is deleted and anyway cleaned_data is not used for display.  To change what is displayed, you need to change the value in the form's data dictionary.  The above code would work if you changed where you have 'cleaned_data["a34_descricao"] = whatever' (btw I don't see where you have set cleaned_data...somewhere you must have a cleaned_data = self.cleaned_data?) to 'self.data["a34_descricao"] = whatever".

If you are going to do this the data dictionary you pass in on form creation must be modifiable, so you will need to pass in a copy of request.GET or request.POST, since those are read-only.

Also, if you are going to do this it would be polite to include the original value that was "corrected" in the error message.  That way the user has a chance to compare what they submitted to what is "correct" and see the difference.  Leaving the submitted value out of the message, I fear, will lead to some "huh?" reactions from users when the corrected value is what they think they typed in the first place.

 
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): 
[snip rest]
 
I don't know that you can do this from a field subclass clean() method.  You need access to the form, and you don't have that for a general field validation method.

Karen
Reply all
Reply to author
Forward
0 new messages