pythonvalidationflaskflask-wtformswtforms

Flask WTForm DateField to only accept today and future dates only


I have a WT form, which requires data input from the front end side. Currently, the form allows the user to enter all dates but as I am creating a site which should only allow future dates to be input, I would like to add a validator to this, to not allow past dates.

I have had a look at previous questions, however, they have not been of great help.

Below is a section from my forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, DateField
from wtforms.validators import DataRequired
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from ..models import TasterDay, Course


class TasterDayForm(FlaskForm):
"""
Form for admin to add or edit a taster day
"""
name = StringField('Name', validators=[DataRequired()])
date = DateField('Date (DD-MM-YYYY)', format='%d-%m-%Y')
course = StringField('Course', validators=[DataRequired()])
lecturer = StringField('Lecturer', validators=[DataRequired()])
description = StringField('Description', validators=[DataRequired()])
submit = SubmitField('Submit')

Here is a section from my views.py file which shows some of the routes.

from flask import abort, flash, redirect, render_template, url_for
from flask_login import current_user, login_required

from . import admin
from .forms import TasterDayForm, StudentAssignForm, CourseForm
from .. import db
from ..models import TasterDay, Course, Student


def check_admin():
"""
Prevent non-admins from accessing the page
"""
if not current_user.is_admin:
    abort(403)


 # TasterDay Views

@admin.route('/tasterdays', methods=['GET', 'POST'])
@login_required
def list_tasterdays():
"""
List all tasterdays
"""
check_admin()

tasterdays = TasterDay.query.all()

return render_template('admin/tasterdays/tasterdays.html',
                       tasterdays=tasterdays, title="TasterDay")


@admin.route('/tasterdays/add', methods=['GET', 'POST'])
@login_required
def add_tasterdays():
"""
Add a Tasterday to the database
"""
check_admin()

add_tasterdays = True

form = TasterDayForm()
if form.validate_on_submit():
    tasterdays = TasterDay(name=form.name.data,
                            description=form.description.data,
                           date=form.date.data,
                           course=form.course.data,
                           lecturer=form.lecturer.data)

    try:
        # add tasterday to the database
        db.session.add(tasterdays)
        db.session.commit()
        flash('You have successfully added a new Taster Day.')
    except:
        # in case tasterday  already exists
        flash('Error: Taster Day already exists.')

    # redirect to tasterdays page
    return redirect(url_for('admin.list_tasterdays'))

# load tasterdays template
return render_template('admin/tasterdays/tasterday.html', action="Add",
                       add_tasterdays=add_tasterdays, form=form,
                       title="Add Taster Day")


@admin.route('/tasterdays/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_tasterdays(id):
"""
Edit a TasterDay
"""
check_admin()

add_tasterdays = False

tasterdays = TasterDay.query.get_or_404(id)
form = TasterDayForm(obj=tasterdays)
if form.validate_on_submit():
    tasterdays.name = form.name.data
    tasterdays.description = form.description.data
    tasterdays.date = form.date.data
    tasterdays.course = form.course.data
    tasterdays.lecturer = form.lecturer.data
    db.session.commit()
    flash('You have successfully edited the TasterDay.')

    # redirect to the tasterdays page
    return redirect(url_for('admin.list_tasterdays'))

form.description.data = tasterdays.description
form.name.data = tasterdays.name
form.date.data = tasterdays.date
form.course.data = tasterdays.course
form.lecturer.data = tasterdays.lecturer
return render_template('admin/tasterdays/tasterday.html', action="Edit",
                       add_tasterdays=add_tasterdays, form=form,
                       tasterdays=tasterdays, title="Edit TasterDay")

Could someone please explain what I need to add to ensure that past dates cannot be input in the form, and if is, throws an error.

Thank you


Solution

  • I managed to figure out the answer myself.

    I added import datetime to the top of my forms.py page.

    Just below my class TasterDayForm(FlaskForm):

    I added:

        def validate_date(form, field):
        if field.data < datetime.date.today():
            raise ValidationError("The date cannot be in the past!")
    

    This works, and throws an error if the user enters a date which has passed.