Refactored form model for custom validators
This commit is contained in:
parent
ba082d4ed7
commit
8f8a12b609
@ -1,9 +1,11 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from flask_wtf.file import FileField, FileRequired, FileAllowed
|
||||
from wtforms import StringField, PasswordField, BooleanField, DateField, SelectField
|
||||
from wtforms import StringField, PasswordField, BooleanField, DateField, SelectField, IntegerField
|
||||
from wtforms.validators import InputRequired, Email, Length, EqualTo, Optional
|
||||
from datetime import date, timedelta
|
||||
|
||||
from .validators import value
|
||||
|
||||
class LoginForm(FlaskForm):
|
||||
username = StringField('Username', validators=[InputRequired(), Length(min=4, max=15)])
|
||||
password = PasswordField('Password', validators=[InputRequired(), Length(min=6, max=30, message='The password must be between 6 and 20 characters long.')])
|
||||
@ -54,3 +56,7 @@ class CreateTest(FlaskForm):
|
||||
class UploadDataForm(FlaskForm):
|
||||
data_file = FileField('Data File', validators=[FileRequired(), FileAllowed(['json'])])
|
||||
default = BooleanField('Make Default', render_kw={'checked': True})
|
||||
|
||||
class AddTimeAdjustment(FlaskForm):
|
||||
time = IntegerField('Extra Time (Minutes)', validators=[InputRequired(), value(max=60)])
|
||||
|
11
ref-test/admin/models/forms/validators.py
Normal file
11
ref-test/admin/models/forms/validators.py
Normal file
@ -0,0 +1,11 @@
|
||||
from wtforms.validators import ValidationError
|
||||
|
||||
def value(min=0, max=None):
|
||||
message = f'Value must be between {min} and {max}.'
|
||||
|
||||
def _length(form, field):
|
||||
value = field.data or 0
|
||||
if value < min or max != None and value > max:
|
||||
raise ValidationError(message)
|
||||
|
||||
return _length
|
Loading…
Reference in New Issue
Block a user