views:

2276

answers:

2

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
+2  A: 

change self data in the clean method to change the value which gets displayed

This works, but is undocumented and it feels ugly to modify the QueryDict object directly. Additionally, I had to write a helper function to modify the read-only QueryDict....
Yes right but there's afaik no official way to do this.
A: 

If your form is a model form a better approach would be to get an instance and correct that data instead:

inst = my_form.save(commit=False)
if inst.a34_stuff is None: #or incorrect
    inst.a34_stuff = "corrected"
    request.user.message_set.create(message = "Error corrected")
    return HttpResponseRedirect(reverse('your_url_name',
   args=[])

PS: I am not sure if this will actually work... Not sure if form will pickup changes.

drozzy