|
1 | | -# Flask-WTF(WTForms) integration |
| 1 | +# WTForms integration |
2 | 2 |
|
3 | 3 | ```{important} |
4 | 4 | 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: |
55 | 55 | {attr}`label`, otherwise pure field name used. |
56 | 56 | - If model field definition have {attr}`help_text` it will be used as form field |
57 | 57 | {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. |
60 | 64 | - Field's {attr}`choices`, if exist, used as form {attr}`choices`. |
61 | 65 |
|
62 | 66 | ```{warning} |
@@ -131,7 +135,51 @@ Not yet documented. Please help us with new pull request. |
131 | 135 |
|
132 | 136 | ### StringField |
133 | 137 |
|
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` |
135 | 183 |
|
136 | 184 | ### URLField |
137 | 185 |
|
|
0 commit comments