0

my form is valid first and working, but when I add choiceField to form and use select in template its not valid anymore. form.is_valid gives false

in form.py i added line:

     crossover_select = forms.ChoiceField(label="crossover_select")

in template I added these:

        <select name="crossover_select">
            <option value={{crossover}}>old value {{crossover}}</option>
            <option value={{peak}}>Peak {{peak}}</option>
            <option value={{median}}>Median {{median}}</option>
            <option value="Other">Other</option>
        </select>

those crossover, peak and median are float.

now in views.py when I have:

     if request.method=='POST':
          form = myForm(request.POST)
          print form.is_valid()

and that gives false

so my question is that where does this go wrong? without those changes everything works, but when I will do that drop down the form is not valid anymore

TheLaama
  • 307
  • 2
  • 4
  • 12

2 Answers2

0

You need to specify choices to the form field, otherwise, it will not validate the form.

Add something like:

CROSSOVER_CHOICES = ( (1.0, "Old value 1.0"),
                      (2.0, "Peak Two"),
                    )

crossover_select = forms.ChoiceField(label="crossover_select", 
                                       choices=CROSSOVER_CHOICES)

Note that, values specified in CROSSOVER_CHOICES should match to the submitted value through form. For that its better to render this field, instead of manually coding choices values in html.

So instead of these lines in html

<select name="crossover_select">
        <option value={{crossover}}>old value {{crossover}}</option>
        <option value={{peak}}>Peak {{peak}}</option>
        <option value={{median}}>Median {{median}}</option>
        <option value="Other">Other</option>
</select>

Do

{{ form.crossover_select }}
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • I can't put my choices to form field cos the values will change a lot and many times – TheLaama Aug 08 '13 at 09:07
  • @user2108375, you can update the choices in forms `__init__()` method, there are many examples here, one of them http://stackoverflow.com/questions/3419997/creating-a-dynamic-choice-field – Rohan Aug 08 '13 at 10:11
  • ah okay.. well thanks... now I know a bit better about that. My problem is that I will have three choice fields but now I know better what to do so meaby I will find some answers – TheLaama Aug 08 '13 at 10:50
0

Let me say like this way.

Whenever adding a ChoiceField in form.py try to add it in init.

Ex:

def _ _init _ _(self, *args, **kwargs):

        super(<class_name>, self).__init__(*args, **kwargs)
        crossover_select = forms.ChoiceField(label="crossover_select")

And in your views.py

if request.method=='POST':

      #request.POST has all choice field values in this format
      {'xyz': [u'This field is required.']}
      # change it to {'xyz': 'This field is required.'} by updating 
      the request.POST
      # import copy
      data = copy.deepcopy(dict(request.POST))
      update this dict(data) and pass it to constructor
      form = myForm(data)
      print form.is_valid()
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103