@@ -217,6 +217,148 @@ Note this won't have behavior fields added to it at this stage, only the fields
217
217
- {doc}` reference list of fields used in Plone </backend/fields> `
218
218
219
219
220
+ (backend-schemas-directives-label)=
221
+
222
+ ## Using schema directives
223
+
224
+ With ` plone.autoform ` and ` plone.supermodel ` we can use directives to add information to the schema fields.
225
+
226
+
227
+ ### Omitting fields
228
+
229
+ A field can be omitted entirely from all forms, or from some forms, using the ` omitted ` and ` no_omit ` directives.
230
+ In this example, the ` dummy ` field is omitted from all forms, and the ` edit_only ` field is omitted from all forms except those that provide the IEditForm interface:
231
+
232
+ ``` {code-block} python
233
+ :emphasize-lines: 7,12,13
234
+ :linenos:
235
+
236
+ from z3c.form.interfaces import IEditForm
237
+ from plone.supermodel import model
238
+ from plone.autoform import directives as form
239
+
240
+ class IMySchema(model.Schema):
241
+
242
+ form.omitted('dummy')
243
+ dummy = schema.Text(
244
+ title=u"Dummy"
245
+ )
246
+
247
+ form.omitted('edit_only')
248
+ form.no_omit(IEditForm, 'edit_only')
249
+ edit_only = schema.TextLine(
250
+ title = u'Only included on edit forms',
251
+ )
252
+ ```
253
+
254
+ In supermodel XML, this can be specified as:
255
+
256
+ ``` {code-block} xml
257
+ :emphasize-lines: 3,9
258
+
259
+ <field type="zope.schema.TextLine"
260
+ name="dummy"
261
+ form:omitted="true">
262
+ <title>Dummy</title>
263
+ </field>
264
+
265
+ <field type="zope.schema.TextLine"
266
+ name="edit-only"
267
+ form:omitted="z3c.form.interfaces.IForm:true z3c.form.interfaces.IEditForm:false">
268
+ <title>Only included on edit form</title>
269
+ </field>
270
+ ```
271
+
272
+ ` form:omitted ` may be either a single boolean value, or a space-separated list of form_interface: boolean pairs.
273
+
274
+
275
+ ### Re-ordering fields
276
+
277
+ A field's position in the form can be influenced using the ` order_before ` and ` order_after ` directives.
278
+ In this example, the ` not_last ` field is placed before the ` summary ` field even though it is defined afterward:
279
+
280
+ ``` {code-block} python
281
+ :emphasize-lines: 12
282
+ :linenos:
283
+
284
+ from plone.supermodel import model
285
+ from plone.autoform import directives as form
286
+
287
+ class IMySchema(model.Schema):
288
+
289
+ summary = schema.Text(
290
+ title=u"Summary",
291
+ description=u"Summary of the body",
292
+ readonly=True
293
+ )
294
+
295
+ form.order_before(not_last='summary')
296
+ not_last = schema.TextLine(
297
+ title=u"Not last",
298
+ )
299
+ ```
300
+
301
+ The value passed to the directive may be either '* ' (indicating before or after all fields) or the name of another field.
302
+ Use ` '.fieldname' ` to refer to field in the current schema or a base schema.
303
+ Prefix with the schema name (e.g. ` 'IDublinCore.title' ` ) to refer to a field in another schema.
304
+ Use an unprefixed name to refer to a field in the current or the default schema for the form.
305
+
306
+ In supermodel XML, the directives are called ` form:before ` and ` form:after ` .
307
+ For example:
308
+
309
+ ``` {code-block} xml
310
+ :emphasize-lines: 3
311
+
312
+ <field type="zope.schema.TextLine"
313
+ name="not_last"
314
+ form:before="*">
315
+ <title>Not last</title>
316
+ </field>
317
+ ```
318
+
319
+
320
+ ### Organizing fields into fieldsets
321
+
322
+ Fields can be grouped into fieldsets, which will be rendered within an HTML ` <fieldset> ` tag.
323
+ In this example the ` footer ` and ` dummy ` fields are placed within the ` extra ` fieldset:
324
+
325
+ ``` {code-block} python
326
+ :emphasize-lines: 6,7,8,9
327
+ :linenos:
328
+
329
+ from plone.supermodel import model
330
+ from plone.autoform import directives as form
331
+
332
+ class IMySchema(model.Schema):
333
+
334
+ model.fieldset('extra',
335
+ label=u"Extra info",
336
+ fields=['footer', 'dummy']
337
+ )
338
+
339
+ footer = schema.Text(
340
+ title=u"Footer text",
341
+ )
342
+
343
+ dummy = schema.Text(
344
+ title=u"Dummy"
345
+ )
346
+ ```
347
+
348
+ In supermodel XML fieldsets are specified by grouping fields within a ` <fieldset> ` tag:
349
+
350
+ ``` xml
351
+ <fieldset name =" extra" label =" Extra info" >
352
+ <field name =" footer" type =" zope.schema.TextLine" >
353
+ <title >Footer text</title >
354
+ </field >
355
+ <field name =" dummy" type =" zope.schema.TextLine" >
356
+ <title >Dummy</title >
357
+ </field >
358
+ </fieldset >
359
+ ```
360
+
361
+
220
362
## Advanced
221
363
222
364
``` {note}
@@ -588,17 +730,4 @@ def fields(self):
588
730
f.field = schema_field
589
731
```
590
732
591
- #### Don't use dict ` {} ` or list ` [] ` as a default value
592
-
593
- Because of how Python object construction works, giving ` [] ` or ` {} ` as a default value will make all created field values share this same object.
594
-
595
- ``` {seealso}
596
- [The Hitchhiker's Guide to Python, Common Gotchas](https://docs.python-guide.org/writing/gotchas)
597
- ```
598
-
599
- Use value adapters instead.
600
-
601
- ``` {seealso}
602
- [`plone.directives.form` documentation of Value adapters](https://pypi.org/project/plone.directives.form/#value-adapters)
603
- ```
604
733
0 commit comments