I've produced a select field for staff to choose their name. My goal for when the user does not select their name, but instead leaves the drop down box on the selection "---", In this scenario I want the ValidationError to then print it's message. At the moment nothing is printed when I test this.
Here is my forms.py files:
from flask_wtf import Form
from wtforms import StringField, BooleanField, SelectField
from wtforms.validators import DataRequired, Required, ValidationError
class StaffNames(Form):
staff = SelectField(
'staff',
choices=[("", "---"), ('1', 'John Jones'), ('2', 'Chris Hughes'), ('3', 'Lyn Fox')],
)
def validate_staff(form, field):
if field.data is "---":
raise ValidationError("Sorry, you havn't chosen a staff name")
Here is my view.py file:
from flask import render_template, flash, redirect
from app import app
from .forms import StaffNames
@app.route('/')
@app.route('/index', methods=['GET', 'POST'])
def index():
form = StaffNames()
if form.validate_on_submit():
return redirect('/results')
return render_template('index.html',
title='RA Index Search',
form=form)
here is my index.html which relates to this form:
<!-- extend base layout -->
{% extends "base.html" %}
{% block content %}
<center>
<h1>Search</h1>
<form action="" method="post" name="login">
<p>{{ form.staff }} Select your name</p>
<p><input type="submit" value="Search"></p>
</form>
{% endblock %}
Can anyone see what I am doing wrong or suggest an alternative that won't melt my brain! lol Thanks