On May 24, 4:30 am, forthfan <[email protected]> wrote:
>
> from django import forms
> from django.core.validators import *
>
> class IdentForm(forms.Form):
>   ident = forms.CharField()
>   ident_type = forms.ChoiceField(choices=(
>       ('EIN','Employer ID'),
>       ('SSN','Social Security Number'),
>   ))
>
>   TYPE_CHOICES = (
>       ('EIN','\d{2}-\d{7}'),
>       ('SSN','\d{3}-\d{2}-\d{4}'),
>   )
>
>   def clean(self):
>     cleaned_data = super(IdentForm, self).clean()
>     ident = cleaned_data.get('ident')
>     ident_type = cleaned_data.get('ident_type')

Don't assume you'll have values for both fields in cleaned_data - they
may have failed validation already.


>     regexp = TYPE_CHOICES[ident_type]

I assume you meant:

      regexp = self.TYPE_CHOICES[ident_type]

but it wont work since TYPE_CHOICES is a list of tuples, not a dict,
so this should be:


      regexp = dict(self.TYPE_CHOICES)[ident_type]

>     RegexValidator([regexp]) # What's missing?

First, you have to get rid of the [] here - RegexpValidator expects
either a string or compiled regexp as first argument.

Then once you have a validator instance, you have to call it (like you
would call a function)

>     # How do I pass ident to the validator?

As argument to the call.

>     if ??????: # Do I test for existence of error message?

Validators raise a django.core.exceptions.ValidationError

>       ident = ''
>     return cleaned_data

Rebinding the 'indent' symbol in the current namespace won't change
the value (if any) of cleaned_data['ident']. You want to modify
cleaned_data itself (and either set the erreor message manually in
either ident and/or ident_type fields errors or reraise the
ValidationError)

class IdentForm(forms.Form):
  ident = forms.CharField()
  ident_type = forms.ChoiceField(choices=(
      ('EIN','Employer ID'),
      ('SSN','Social Security Number'),
  ))

  TYPE_CHOICES = (
      ('EIN','\d{2}-\d{7}'),
      ('SSN','\d{3}-\d{2}-\d{4}'),
  )

 # no need to reinstanciate validators on each call, we can as well
 # do the job here once for all
 TYPE_VALIDATORS = dict((key, RegexpValidator(regexp)) for key, regexp
in TYPE_CHOICES)

 def clean(self):
    cleaned_data = super(IdentForm, self).clean()
    ident = cleaned_data.get('ident', '')
    ident_type = cleaned_data.get('ident_type', '')
    if ident and ident_type:
       # we assume that ident_type is ok here - if not there's
       # a serious problem with forms.ChoiceField ;)
       validate = self.TYPE_VALIDATORS[ident_type]
       try:
           validate(ident)
       except ValidationError, e:
           # doing the simplest thing here, so the error will appear
           # in non-field errors. If you want to set the error on
           # the ident and/or ident_type field(s), cf the FineManual:
           # 
https://docs.djangoproject.com/en/1.3/ref/forms/validation/#described-later
           del self.cleaned_data['ident']
           raise

    return cleaned_data

HTH

-- 
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.

Reply via email to