flask chained dropdown with selectfield Not a valid choice flask chained dropdown with selectfield Not a valid choice flask flask

flask chained dropdown with selectfield Not a valid choice


You must also fill in the form.area.choices before validation. Or else there is no valid choice for area ... and so you've got an error message if your form provide one.

@users.route("/finish_registration", methods=['GET', 'POST'])def finish_registration():    cur = mysql.connection.cursor()    result1 = cur.execute("select * from region")    results = cur.fetchall()    form = RegistrationDDForm(request.form)    form.region.choices = [('0', 'Select Region')] +         [(x['region_id'], x['region']) for x in results]    if request.method == 'POST':        cur = mysql.connection.cursor()        results = cur.execute("select * from area where region_id=%s", request.form.region_id)        results = cur.fetchall()        # Add choices to area to allow validation        form.area.choices = [(x['area_id'], x['area']) for x in results]        if form.validate():            # Eventually add data to database table            pass    return render_template('finish_registration.html', title='Register', form=form)