Skip to content

Commit 131b3d8

Browse files
committed
Add StringField forms documentation
1 parent c4bb72f commit 131b3d8

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

docs/forms.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Flask-WTF(WTForms) integration
1+
# WTForms integration
22

33
```{important}
44
Documentation below is related to project version 2.0.0 or higher, old versions has
@@ -55,8 +55,12 @@ For all fields, processed by Flask-Mongoengine integration:
5555
{attr}`label`, otherwise pure field name used.
5656
- If model field definition have {attr}`help_text` it will be used as form field
5757
{attr}`description`, otherwise empty string used.
58-
- Field's {attr}`default` used as form {attr}`default`, that's why for string fields
59-
special {class}`~.NoneStringField` with `None` value support used.
58+
- Field's {attr}`default` used as form {attr}`default`, that's why special WTForms
59+
fields implementations was created. Details can be found in
60+
{mod}`flask_mongoengine.wtf.fields` module. In new form generator only 'Mongo'
61+
prefixed classes are used for fields, other classes are deprecated and will be
62+
removed in version **3.0.0**. If you have own nesting classes, you should check
63+
inheritance and make an update.
6064
- Field's {attr}`choices`, if exist, used as form {attr}`choices`.
6165

6266
```{warning}
@@ -131,7 +135,51 @@ Not yet documented. Please help us with new pull request.
131135

132136
### StringField
133137

134-
Not yet documented. Please help us with new pull request.
138+
- API: {class}`.db_fields.StringField`
139+
- Default form field class: Selected by field settings combination
140+
141+
#### Form generation behaviour
142+
143+
By default, during WTForm generation for fields without specified size (
144+
{attr}`min_length` or {attr}`max_length`) class {class}`.MongoTextAreaField` is used,
145+
in case when {attr}`min_length` or {attr}`max_length` set, then
146+
{class}`.MongoStringField` used and {class}`~wtforms.validators.Length` will be added
147+
to form field validators. This allows to keep documents of any size in mongodb.
148+
149+
In some cases class {class}`~.MongoStringField` is not the best choice for field, even
150+
with limited size. In this case user can easily overwrite generated field class by
151+
providing {attr}`wtf_field_class` on {class}`.db_fields.StringField` field declaration,
152+
as on document, as well as on form generation steps.
153+
154+
If database field definition has {attr}`regex` parameter set, then
155+
{class}`~wtforms.validators.Regexp` validator will be added to the form field.
156+
157+
#### Features deprecated
158+
159+
Field declaration step keyword arguments {attr}`password` and {attr}`textarea` are
160+
deprecated in Flask-Mongoengine version **2.0.0** and exist only to make migration
161+
steps easy.
162+
163+
To implement same behaviour, user should use {attr}`wtf_field_class` setting on
164+
{class}`.db_fields.StringField` init.
165+
166+
#### Related WTForm custom fields
167+
168+
Several special WTForms field implementation was created to support mongodb database
169+
behaviour and do not create any values in database, in case of empty fields. They
170+
can be used as {attr}`wtf_field_class` setting or independently. Some of them used
171+
in another database fields too, but all of them based on
172+
{class}`wtforms.fields.StringField` and {class}`~.EmptyStringIsNoneMixin`. You can use
173+
{class}`~.EmptyStringIsNoneMixin` for own field types.
174+
175+
- {class}`~.MongoEmailField`
176+
- {class}`~.MongoHiddenField`
177+
- {class}`~.MongoPasswordField`
178+
- {class}`~.MongoSearchField`
179+
- {class}`~.MongoStringField`
180+
- {class}`~.MongoTelField`
181+
- {class}`~.MongoTextAreaField`
182+
- {class}`~.MongoURLField`
135183

136184
### URLField
137185

0 commit comments

Comments
 (0)