Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/wtforms/fields/simple.py: 72%
53 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:32 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:32 +0000
1from .. import widgets
2from .core import Field
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)
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``
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 """
33 widget = widgets.CheckboxInput()
34 false_values = (False, "false", "")
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
41 def process_data(self, value):
42 self.data = bool(value)
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
50 def _value(self):
51 if self.raw_data:
52 return str(self.raw_data[0])
53 return "y"
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 """
62 widget = widgets.TextInput()
64 def process_formdata(self, valuelist):
65 if valuelist:
66 self.data = valuelist[0]
68 def _value(self):
69 return str(self.data) if self.data is not None else ""
72class TextAreaField(StringField):
73 """
74 This field represents an HTML ``<textarea>`` and can be used to take
75 multi-line input.
76 """
78 widget = widgets.TextArea()
81class PasswordField(StringField):
82 """
83 A StringField, except renders an ``<input type="password">``.
85 Also, whatever value is accepted by this field is not rendered back
86 to the browser like normal fields.
87 """
89 widget = widgets.PasswordInput()
92class FileField(Field):
93 """Renders a file upload field.
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 """
101 widget = widgets.FileInput()
103 def _value(self):
104 # browser ignores value of file input for security
105 return False
108class MultipleFileField(FileField):
109 """A :class:`FileField` that allows choosing multiple files."""
111 widget = widgets.FileInput(multiple=True)
113 def process_formdata(self, valuelist):
114 self.data = valuelist
117class HiddenField(StringField):
118 """
119 HiddenField is a convenience for a StringField with a HiddenInput widget.
121 It will render as an ``<input type="hidden">`` but otherwise coerce to a string.
122 """
124 widget = widgets.HiddenInput()
127class SubmitField(BooleanField):
128 """
129 Represents an ``<input type="submit">``. This allows checking if a given
130 submit button has been pressed.
131 """
133 widget = widgets.SubmitInput()
136class SearchField(StringField):
137 """
138 Represents an ``<input type="search">``.
139 """
141 widget = widgets.SearchInput()
144class TelField(StringField):
145 """
146 Represents an ``<input type="tel">``.
147 """
149 widget = widgets.TelInput()
152class URLField(StringField):
153 """
154 Represents an ``<input type="url">``.
155 """
157 widget = widgets.URLInput()
160class EmailField(StringField):
161 """
162 Represents an ``<input type="email">``.
163 """
165 widget = widgets.EmailInput()
168class ColorField(StringField):
169 """
170 Represents an ``<input type="color">``.
171 """
173 widget = widgets.ColorInput()