On Nov 19, 12:30 pm, Milan Andric <[EMAIL PROTECTED]> wrote:
> On Nov 19, 12:16 pm, Milan Andric <[EMAIL PROTECTED]> wrote:
>
> > On Nov 19, 12:13 pm, RajeshD <[EMAIL PROTECTED]> wrote:
>
> > > > I would like to validate a field if another field is defined. So i
> > > > have two fields (state and state (international) ) -- one of them
> > > > needs to be defined. How would i do this with newforms?
>
> > > By adding a clean() method to your form class. See the 3rd cleaning
> > > method described here:
>
> > >http://www.djangoproject.com/documentation/newforms/#custom-form-and-...
>
> > Ah, override the Form's clean method as opposed to the field's. That
> > makes sense.
>
> Found a good thread on it, thank you.
>
> http://groups.google.com/group/django-users/browse_thread/thread/2ab9...
>
Here's what I came up with ...
def clean(self):
"""
Do some special validation on biz_state and biz_zip fields so
either it
or international fields are defined.
"""
try:
if self.cleaned_data['biz_state']:
# just make sure it's not empty string
pass
else:
try:
if self.cleaned_data['biz_state_intl']:
# just make sure it's not empty string
pass
else:
# if we got this far that means the two fields
#validated (are clean) but empty. set the
errors.
self._errors['biz_state'] = [
u'Please define a US State or
International State.'
]
self._errors['biz_state_intl'] = [
u'Please define a US State or International
State.'
]
except KeyError:
# biz_state_intl wasn't clean. let the parent
class do its
# thing.
pass
except KeyError:
# biz_state wasn't clean. let the parent class do its
thing.
pass
try:
if self.cleaned_data['biz_zip']:
# just make sure it's not empty string
pass
else:
try:
if self.cleaned_data['biz_zip_intl']:
# just make sure it's not empty string
pass
else:
# if we got this far that means the two fields
#validated (are clean) but empty. set the
errors.
self._errors['biz_zip'] = [
u'Please define a US State or
International Zipcode.'
]
self._errors['biz_zip_intl'] = [
u'Please define a US State or
International Zipcode.'
]
except KeyError:
# biz_zip_intl wasn't clean. let the parent class
do its
# thing at the bottom.
pass
except KeyError:
# biz_zip wasn't clean. let the parent class do its thing.
pass
return super(AppFormPart1, self).clean()
--
Milan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---