Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to set the data attribute of the field to '' in your clean method:</p> <pre><code>class MyForm(forms.Form): name = forms.CharField(max_length=50) def clean_name(self): name = self.cleaned_data['name'] if name == 'Bob': raise forms.ValidationError(u'Name cannot be "Bob"') self.data['name'] = '' return name </code></pre> <p>Hope that helps you out.</p> <p>[Edit]</p> <p>Here's an expanded example that is working for me in Django 1.3.</p> <pre><code>#models.py from django.db import models class ContactRequest(models.Model): name = models.CharField(max_length=100) email = models.EmailField() subject = models.CharField(max_length=255) message = models.TextField() response_returned = models.BooleanField(default=False) def __unicode__(self): return self.name #forms.py class ContactRequestForm(forms.ModelForm): class Meta: model = ContactRequest exclude = ('response_returned',) def clean_email(self): email = self.cleaned_data['email'] if email != 'test@test.com': self.data['email'] = '' raise forms.ValidationError(u'Email is not test@test.com') return email </code></pre> <p>[Edit] Here's an additional example that clears all fields if a condition isn't met in a clean method. This works for me in Django 1.1.4.</p> <pre><code>#forms.py from django import forms class ContactForm(forms.Form): name = forms.CharField(max_length=50) email = forms.EmailField() def clean(self): cleaned_data = self.cleaned_data name = cleaned_data.get('name') email = cleaned_data.get('email') if email == 'test@test.com' and name == 'test': for k, _ in self.fields.iteritems(): self.data[k] = '' raise forms.ValidationError(u'Email cannot be "test@test.com" and name cannot be "test"') return cleaned_data </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload