views:

57

answers:

2

so...i've been banging my head on this for a bit. I'm getting the most bizarre error when attempting to validate a form. I pass input to the form wanting to test behavior when the form fails validation, i.e. i expect it to fail validation.

i've got this code for a form:

class CTAForm(forms.Form):      
    first_name = forms.CharField(max_length=50)
    email = forms.EmailField()
    phone = forms.CharField(max_length=15, required=False, label='Phone (optional)')

And i make this call somewhere in a view in order to process the form data.

if cta_form.is_valid():

and i get:

Exception Type:     TypeError
Exception Value:    'str' object is not callable
Exception Location:     /usr/lib/pymodules/python2.6/django/forms/forms.py in full_clean, line 246

Traceback shows this:

/usr/lib/pymodules/python2.6/django/forms/forms.py in full_clean:

  1. except ValidationError, e:
  2. self._errors[name] = self.error_class(e.messages) ...

▼ Local vars
Variable Value
e: ValidationError()
field: django.forms.fields.EmailField object at <0x225c4d0c>
name: 'email'
self: blah.views.CTAForm object at <0x2245b42c>
value: u' '

I'm feeling quite stupid. Anyone provide me with guidance on this issue? thanks.


edit: more complete view code as requested

def cta_add(request):
    context_dict = {'categories': Category.objects.all().order_by('order'),}
    # we always expect to receive data via post
    if request.method =='POST':
        # get the current vid based on the parameter        
        current_vid = Video.objects.get(slug=request.POST['current_vid'])           
        # use this video to create our dynamic form so we get the valid cta values based on that video
        cta_form_class = make_cta_form(current_vid.cta_set.all().order_by('order'))
        # instantiate our new dynamic form based on the post data
        cta_form = cta_form_class(request.POST,  error_class='error')

        # if we got this far, we were able to generate a form to validate the user's input against.
        # If form validates, than process and take user to success screen           
        if cta_form.is_valid():

helper function make_cta_form below

def make_cta_form(cta_set):
    cta_list =[]
    if not cta_set is None:
        for cta in cta_set:
            cta_list.append((cta.text, cta.text))

    class CTAForm(forms.Form):
        cta = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=cta_list, label='', required=False)
        first_name = forms.CharField(max_length=50)
        email = forms.EmailField()
        phone = forms.CharField(max_length=15, required=False, label='Phone (optional)')        

    return CTAForm
A: 

In your views.py file. When you get the form (after the user fills up). Is this what you do?

from your_app.forms import *

def process_form(request):
    cta_form = CTAForm(request.POST)

    if cta_form.is_valid():
        # your code.

You need to pass in the POST for your form as an initializer for the constructor.

MovieYoda
yes i do do that. =|
w-
+1  A: 

You get an error about a string not being callable, but your errorclass for your form is a string, not an error class.

    cta_form = cta_form_class(request.POST,  error_class='error')

Now the only reference in the docs I can find for error_class is as a list of strings, so you may just try

    cta_form = cta_form_class(request.POST,  error_class=['error'])

Though it seems to make more sense to do something like

    import forms 
    [...]
    cta_form = cta_form_class(request.POST,  error_class=forms.ValidationError("error"))

or possibly:

    cta_form = cta_form_class(request.POST,  error_class=forms.ValidationError)
jimbob
damnit. i knew it was something stupid like this. Thanks Jimbob. I think i was originally thinking that error_class referred to the css class the error would have if it was displayed. I think i figured this out and then forgot all about taking it out of this part of the code. for others looking at this the relevant docs are http://docs.djangoproject.com/en/1.1/ref/forms/api/#customizing-the-error-list-format (1.1 because i'm using 1.1)
w-