base form class and custom field classes
This commit is contained in:
parent
ce763d6493
commit
c2f8da256b
38
forms.py
38
forms.py
@ -1,10 +1,38 @@
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField
|
from wtforms import StringField as _StringField, SubmitField as _SubmitField
|
||||||
from wtforms.validators import DataRequired, Email, Length
|
from wtforms.validators import DataRequired, Email, Length
|
||||||
|
from wtforms.fields.html5 import TelField as _TelField, EmailField as _EmailField
|
||||||
|
|
||||||
|
class AddArgsMixin(object):
|
||||||
|
_field_args = {}
|
||||||
|
|
||||||
|
def __init__(self, name, field_args, *args, **kwargs):
|
||||||
|
self._field_args = field_args
|
||||||
|
super(AddArgsMixin, self).__init__(name, *args, **kwargs)
|
||||||
|
|
||||||
|
class StringField(AddArgsMixin, _StringField):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class SubmitField(AddArgsMixin, _SubmitField):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TelField(AddArgsMixin, _TelField):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class EmailField(AddArgsMixin, _EmailField):
|
||||||
|
pass
|
||||||
|
|
||||||
class ContactForm(FlaskForm):
|
class ContactForm(FlaskForm):
|
||||||
name = StringField('Name', validators=[DataRequired(), Length(min=-1, max=80, message='You cannot have more than 80 characters')])
|
name = StringField('Name', dict(class_='form-control'), validators=[DataRequired(), Length(min=-1, max=80, message='You cannot have more than 80 characters')])
|
||||||
surname = StringField('Surname', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')])
|
surname = StringField('Surname', field_args=dict(class_='form-control'), validators=[
|
||||||
email = StringField('E-Mail', validators=[Email(), Length(min=-1, max=200, message='You cannot have more than 200 characters')])
|
Length(min=-1, max=100, message='You cannot have more than 100 characters')])
|
||||||
phone = StringField('Phone', validators=[Length(min=-1, max=20, message='You cannot have more than 20 characters')])
|
email = EmailField('E-Mail', field_args=dict(class_='form-control'), validators=[
|
||||||
|
Email(), Length(min=-1, max=200, message='You cannot have more than 200 characters')])
|
||||||
|
phone = TelField('Phone', field_args=dict(class_='form-control'), validators=[
|
||||||
|
Length(min=-1, max=20, message='You cannot have more than 20 characters')])
|
||||||
|
add = SubmitField('Add', field_args=dict(class_='btn btn-success'))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user