On 11/2/07, crybaby <[EMAIL PROTECTED]> wrote: > > > I have a form and trying to do validation. When I submit a form > without any values into the field, "reason", I get this error. > > Exception Type: KeyError > Exception Value: 'reason' > > at this line of ReasonForm: > > if(self.cleaned_data['reason']==''): > > here is my ReasonForm: > class ReasonForm(forms.Form): > > reason = forms.CharField(max_length=250) > > def clean(self): > if(self.cleaned_data['reason']==''): > raise forms.ValidationError('Invalid Reason, please enter > it.') > return self.cleaned_data >
The default field validation for a CharField will raise a validation error if the field is required (which yours is, since you have not overridden the default required=True) but empty. When that happens, the field is removed from cleaned_data. So, by the time your form's clean() is run (after the fields have been individually cleaned), 'reason' has already been flagged as an error and is no longer in cleaned_data. In short -- you don't need to be checking for this, as it's already being done by default. Karen --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---