Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Django form

I have a form that fails the is_valid() test. I created an identical form that passes the test with the same data input. Not sure why one would pass and one would fail.

Here are the forms:

choices = ( (1,'Yes'),(0,'No'),
      )

class ActivitySaveForm(forms.Form):
    name = forms.CharField(
        label=u'Activity Name',
        widget=forms.TextInput(attrs={'size': 64})
    )
    url = forms.URLField(
        label=u'URL',
        widget=forms.TextInput(attrs={'size': 64})
    )
    desc = forms.CharField(
        label=u'Describe it',
                widget=forms.TextInput(attrs={'size': 250})
    )
    created = forms.DateField(
        label=u'Date Entered',initial=datetime.date.today,
    )
    priority = forms.CharField(
        label=u'priority: 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    difficulty = forms.CharField(
        label=u'How hard is it? 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    done = forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int
    )   
    tags = forms.CharField(
        label=u'Tags',required=False,
            widget=forms.TextInput(attrs={'size': 64})
    )
#def __init__(self, *args, **kwargs):
    #super(CircuitForm, self).__init__(*args, **kwargs)

    #for key in self.fields:
        #self.fields[key].required = False


class AcTest(forms.Form):
    name = forms.CharField(
        label=u'Activity Name',
        widget=forms.TextInput(attrs={'size': 64})
    )
    url = forms.URLField(
        label=u'URL',
        widget=forms.TextInput(attrs={'size': 64})
    )
    desc = forms.CharField(
        label=u'Describe it',
                widget=forms.TextInput(attrs={'size': 250})
    )
    created = forms.DateField(
        label=u'Date Entered',initial=datetime.date.today,
    )
    priority = forms.CharField(
        label=u'priority: 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    difficulty = forms.CharField(
        label=u'How hard is it? 1-5',
            widget=forms.TextInput(attrs={'size': 1})
    )
    done = forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int
    )   
    tags = forms.CharField(
        label=u'Tags',required=False,
            widget=forms.TextInput(attrs={'size': 64})
    )

Here is the test and the results for both.

>>> data = {'name':'test',
...         'url': 'www.test.com',
...         'desc':'test desc',
...         'created': '01/01/1900',
...         'priority':1,
...         'difficulty':1,
...         'desc':'test desc',
...         'tags':'test desc'}
>>> f=ActivitySaveForm(data)
>>> f.is_valid()
False
>>> f=AcTest(data)
>>> f.is_valid()
True

Not sure how to further diagnose this error and haven't found a way to make it work. Thanks for your help in this.

like image 809
jabs Avatar asked Mar 02 '12 01:03

jabs


1 Answers

I ran your code on and for me, both forms fail to validate. You can just do this: print f.errors and you will get the HTML for the error messages that were created during the validation.

For this I get:

<ul class="errorlist"><li>done<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

So in other words, you didn't specify a value for the 'done' field, even though it was required. Once you add 'done':1 to the data dictionary, the form validates.

As to why one form validated and the other not... I you paste all of this into a single, small program, you'll probably find that both of them behave the same way. I can only speculate that for some reason you didn't really pass the same data in both cases. Or that when you ran the test there was a subtle difference in the forms. But I can't be sure.

At any rate, try printing the errors and you should get an insight.

like image 136
jbrendel Avatar answered Sep 27 '22 17:09

jbrendel