0

I am using the following code in a Flask-wtforms. I have tried with and without various validators in the SelectField but on the first time the user submits the form validation returns false.

I have also tried removing the extra validate method but still leaving a validator in the SelectField and again validation returns False on first submit.

Essentially I want to know if the SelectField is not set to a value of -1 (ie has been populated by the view method and presumably user is happy with the currently active item). I am not sure why the form if valid on second submit even though nothing else has been selected on the form

forms.py

something = SelectField(label = 'Something', choices = [('-1','Select Something')], default=('-1','Select Something'), id='select_something', validators=[validators.DataRequired(message='Nothing selected')])
#NB the line below with no validator also prints "--validate() returned false"
#something = SelectField(label = 'Something', choices = [('-1','Select Something')], default=('-1','Select Something'), id='select_something')
submit = SubmitField('Submit')

def validate(self):
    rv = Form.validate(self)
    if not rv:
        print("--validate() returned false")
        return False

    #the line below never fired, see fix in following line
    #if self.something.data == -1:
    if str(self.something.data) == '-1':
        logger.debug("--validate() data==-1")
        return False
    return True

view.py

form = Something(request.form)
if request.method == 'GET':
    #get tuple_list from database
    ...
    form.something.choices = tuple_list
    form.something.default = tuple_list[0]

if request.method == 'POST' and form.validate():
    print('Something added.')
Don Smythe
  • 9,234
  • 14
  • 62
  • 105
  • 1
    What does `form.errors` say? Have you included `form.csrf_token` in your template and set the `SECRET` key in your config? – MrLeeh Jan 02 '17 at 09:42
  • @MrLeeh, thanks, errors empty, crsf and scret_key are set OK. I found a couple of problems - one with the validation method - data never equalled -1 see fix . Also the was a problem in the request.method == 'GET' block, I removed the while thing and is working. – Don Smythe Jan 02 '17 at 11:33

1 Answers1

1

I was using string instead of integers for the first part of each choices tuple (ie should be (1, 'text')) and not setting default correctly (just set default = n where n = integer).

Note the form.process() call as found here: How do you set a default value for a WTForms SelectField?

Fixes below:

views.py

form = Something(request.form)
#get tuple_list from database
...
form.something.choices = tuple_list
form.something.default = tuple_list[0][0] #integer id value
form.process() 
if request.method == 'POST' and form.validate():
    return self.render_template('it_works.html')
return self.render_template('select_something.html')

forms.py

#no validator used here
something = SelectField(label = 'Something', choices = [], id='select_something')
submit = SubmitField('Submit')

def validate(self):
    if len(self.something.choices) == 0:
        return False
    return True
Community
  • 1
  • 1
Don Smythe
  • 9,234
  • 14
  • 62
  • 105