1from .. import widgets
2from .core import Field
3
4__all__ = (
5 "BooleanField",
6 "TextAreaField",
7 "PasswordField",
8 "FileField",
9 "MultipleFileField",
10 "HiddenField",
11 "SearchField",
12 "SubmitField",
13 "StringField",
14 "TelField",
15 "URLField",
16 "EmailField",
17 "ColorField",
18)
19
20
21class BooleanField(Field):
22 """
23 Represents an ``<input type="checkbox">``. Set the ``checked``-status by using the
24 ``default``-option. Any value for ``default``, e.g. ``default="checked"`` puts
25 ``checked`` into the html-element and sets the ``data`` to ``True``
26
27 :param false_values:
28 If provided, a sequence of strings each of which is an exact match
29 string of what is considered a "false" value. Defaults to the tuple
30 ``(False, "false", "")``
31 """
32
33 widget = widgets.CheckboxInput()
34 false_values = (False, "false", "")
35
36 def __init__(self, label=None, validators=None, false_values=None, **kwargs):
37 super().__init__(label, validators, **kwargs)
38 if false_values is not None:
39 self.false_values = false_values
40
41 def process_data(self, value):
42 self.data = bool(value)
43
44 def process_formdata(self, valuelist):
45 if not valuelist or valuelist[0] in self.false_values:
46 self.data = False
47 else:
48 self.data = True
49
50 def _value(self):
51 if self.raw_data:
52 return str(self.raw_data[0])
53 return "y"
54
55
56class StringField(Field):
57 """
58 This field is the base for most of the more complicated fields, and
59 represents an ``<input type="text">``.
60 """
61
62 widget = widgets.TextInput()
63
64 def process_formdata(self, valuelist):
65 if valuelist:
66 self.data = valuelist[0]
67
68 def _value(self):
69 return str(self.data) if self.data is not None else ""
70
71
72class TextAreaField(StringField):
73 """
74 This field represents an HTML ``<textarea>`` and can be used to take
75 multi-line input.
76 """
77
78 widget = widgets.TextArea()
79
80
81class PasswordField(StringField):
82 """
83 A StringField, except renders an ``<input type="password">``.
84
85 Also, whatever value is accepted by this field is not rendered back
86 to the browser like normal fields.
87 """
88
89 widget = widgets.PasswordInput()
90
91
92class FileField(Field):
93 """Renders a file upload field.
94
95 By default, the value will be the filename sent in the form data.
96 WTForms **does not** deal with frameworks' file handling capabilities.
97 A WTForms extension for a framework may replace the filename value
98 with an object representing the uploaded data.
99 """
100
101 widget = widgets.FileInput()
102
103 def _value(self):
104 # browser ignores value of file input for security
105 return False
106
107
108class MultipleFileField(FileField):
109 """A :class:`FileField` that allows choosing multiple files."""
110
111 widget = widgets.FileInput(multiple=True)
112
113 def process_formdata(self, valuelist):
114 self.data = valuelist
115
116
117class HiddenField(StringField):
118 """
119 HiddenField is a convenience for a StringField with a HiddenInput widget.
120
121 It will render as an ``<input type="hidden">`` but otherwise coerce to a string.
122 """
123
124 widget = widgets.HiddenInput()
125
126
127class SubmitField(BooleanField):
128 """
129 Represents an ``<input type="submit">``. This allows checking if a given
130 submit button has been pressed.
131 """
132
133 widget = widgets.SubmitInput()
134
135
136class SearchField(StringField):
137 """
138 Represents an ``<input type="search">``.
139 """
140
141 widget = widgets.SearchInput()
142
143
144class TelField(StringField):
145 """
146 Represents an ``<input type="tel">``.
147 """
148
149 widget = widgets.TelInput()
150
151
152class URLField(StringField):
153 """
154 Represents an ``<input type="url">``.
155 """
156
157 widget = widgets.URLInput()
158
159
160class EmailField(StringField):
161 """
162 Represents an ``<input type="email">``.
163 """
164
165 widget = widgets.EmailInput()
166
167
168class ColorField(StringField):
169 """
170 Represents an ``<input type="color">``.
171 """
172
173 widget = widgets.ColorInput()