doc/book/en/devweb/edition/form.rst
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Jul 2011 11:56:47 +0200
changeset 7643 f3e3892fc7e3
parent 5869 8a129b3a5aff
child 7901 bdb81b1a8243
permissions -rw-r--r--
[book, form] include complete example of self-posted form with custom field/widget + minor other changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5869
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5465
diff changeset
     1
.. _webform:
8a129b3a5aff reledit refactoring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5465
diff changeset
     2
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
     3
HTML form construction
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
     4
----------------------
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
     5
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
     6
CubicWeb provides the somewhat usual form / field / widget / renderer abstraction
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
     7
to provide generic building blocks which will greatly help you in building forms
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
     8
properly integrated with CubicWeb (coherent display, error handling, etc...),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
     9
while keeping things as flexible as possible.
4443
83ff2bb898a5 start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    10
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    11
A ``form`` basically only holds a set of ``fields``, and has te be bound to a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    12
``renderer`` which is responsible to layout them. Each field is bound to a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    13
``widget`` that will be used to fill in value(s) for that field (at form
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    14
generation time) and 'decode' (fetch and give a proper Python type to) values
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    15
sent back by the browser.
4443
83ff2bb898a5 start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    16
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    17
The ``field`` should be used according to the type of what you want to edit.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    18
E.g. if you want to edit some date, you'll have to use the
5400
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    19
:class:`cubicweb.web.formfields.DateField`. Then you can choose among multiple
b7ab099b128a [doc/book] various content fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5394
diff changeset
    20
widgets to edit it, for instance :class:`cubicweb.web.formwidgets.TextInput` (a
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    21
bare text field), :class:`~cubicweb.web.formwidgets.DateTimePicker` (a simple
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    22
calendar) or even :class:`~cubicweb.web.formwidgets.JQueryDatePicker` (the JQuery
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    23
calendar).  You can of course also write your own widget.
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
    24
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    25
Exploring the available forms
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    26
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    27
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    28
A small excursion into a |cubicweb| shell is the quickest way to
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    29
discover available forms (or application objects in general).
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    30
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    31
.. sourcecode:: python
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    32
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    33
 >>> from pprint import pprint
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    34
 >>> pprint( session.vreg['forms'] )
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    35
 {'base': [<class 'cubicweb.web.views.forms.FieldsForm'>,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    36
           <class 'cubicweb.web.views.forms.EntityFieldsForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    37
  'changestate': [<class 'cubicweb.web.views.workflow.ChangeStateForm'>,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    38
                  <class 'cubes.tracker.views.forms.VersionChangeStateForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    39
  'composite': [<class 'cubicweb.web.views.forms.CompositeForm'>,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    40
                <class 'cubicweb.web.views.forms.CompositeEntityForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    41
  'deleteconf': [<class 'cubicweb.web.views.editforms.DeleteConfForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    42
  'edition': [<class 'cubicweb.web.views.autoform.AutomaticEntityForm'>,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    43
              <class 'cubicweb.web.views.workflow.TransitionEditionForm'>,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    44
              <class 'cubicweb.web.views.workflow.StateEditionForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    45
  'logform': [<class 'cubicweb.web.views.basetemplates.LogForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    46
  'massmailing': [<class 'cubicweb.web.views.massmailing.MassMailingForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    47
  'muledit': [<class 'cubicweb.web.views.editforms.TableEditForm'>],
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    48
  'sparql': [<class 'cubicweb.web.views.sparql.SparqlForm'>]}
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    49
4443
83ff2bb898a5 start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    50
7643
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
    51
The two most important form families here (for all practical purposes) are `base`
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
    52
and `edition`. Most of the time one wants alterations of the
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
    53
:class:`AutomaticEntityForm` to generate custom forms to handle edition of an
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
    54
entity.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    55
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    56
The Automatic Entity Form
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    57
~~~~~~~~~~~~~~~~~~~~~~~~~
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    58
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    59
.. automodule:: cubicweb.web.views.autoform
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    60
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    61
Anatomy of a choices function
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    62
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4465
18fb359f5c7a fix wrong autoclass inclusion
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4443
diff changeset
    63
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    64
Let's have a look at the `ticket_done_in_choices` function given to
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    65
the `choices` parameter of the relation tag that is applied to the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    66
('Ticket', 'done_in', '*') relation definition, as it is both typical
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    67
and sophisticated enough. This is a code snippet from the `tracker`_
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    68
cube.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    69
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    70
.. _`tracker`: http://www.cubicweb.org/project/cubicweb-tracker
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    71
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    72
The ``Ticket`` entity type can be related to a ``Project`` and a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    73
``Version``, respectively through the ``concerns`` and ``done_in``
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    74
relations. When a user is about to edit a ticket, we want to fill the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    75
combo box for the ``done_in`` relation with values pertinent with
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    76
respect to the context. The important context here is:
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    77
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    78
* creation or modification (we cannot fetch values the same way in
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    79
  either case)
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    80
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    81
* ``__linkto`` url parameter given in a creation context
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    82
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    83
.. sourcecode:: python
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
    84
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    85
    from cubicweb.web import formfields
4443
83ff2bb898a5 start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
    86
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    87
    def ticket_done_in_choices(form, field):
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    88
        entity = form.edited_entity
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    89
        # first see if its specified by __linkto form parameters
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    90
        linkedto = formfields.relvoc_linkedto(entity, 'done_in', 'subject')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    91
        if linkedto:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    92
            return linkedto
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    93
        # it isn't, get initial values
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    94
        vocab = formfields.relvoc_init(entity, 'done_in', 'subject')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    95
        veid = None
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    96
        # try to fetch the (already or pending) related version and project
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    97
        if not entity.has_eid():
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    98
            peids = entity.linked_to('concerns', 'subject')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
    99
            peid = peids and peids[0]
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   100
        else:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   101
            peid = entity.project.eid
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   102
            veid = entity.done_in and entity.done_in[0].eid
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   103
        if peid:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   104
            # we can complete the vocabulary with relevant values
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   105
            rschema = form._cw.vreg.schema['done_in'].rdef('Ticket', 'Version')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   106
            rset = form._cw.execute(
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   107
                'Any V, VN ORDERBY version_sort_value(VN) '
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   108
                'WHERE V version_of P, P eid %(p)s, V num VN, '
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   109
                'V in_state ST, NOT ST name "published"', {'p': peid}, 'p')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   110
            vocab += [(v.view('combobox'), v.eid) for v in rset.entities()
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   111
                      if rschema.has_perm(form._cw, 'add', toeid=v.eid)
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   112
                      and v.eid != veid]
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   113
        return vocab
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   114
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   115
The first thing we have to do is fetch potential values from the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   116
``__linkto`` url parameter that is often found in entity creation
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   117
contexts (the creation action provides such a parameter with a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   118
predetermined value; for instance in this case, ticket creation could
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   119
occur in the context of a `Version` entity). The
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   120
:mod:`cubicweb.web.formfields` module provides a ``relvoc_linkedto``
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   121
utility function that gets a list suitably filled with vocabulary
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   122
values.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   123
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   124
.. sourcecode:: python
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   125
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   126
        linkedto = formfields.relvoc_linkedto(entity, 'done_in', 'subject')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   127
        if linkedto:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   128
            return linkedto
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   129
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   130
Then, if no ``__linkto`` argument was given, we must prepare the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   131
vocabulary with an initial empty value (because `done_in` is not
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   132
mandatory, we must allow the user to not select a verson) and already
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   133
linked values. This is done with the ``relvoc_init`` function.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   134
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   135
.. sourcecode:: python
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   136
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   137
        vocab = formfields.relvoc_init(entity, 'done_in', 'subject')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   138
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   139
But then, we have to give more: if the ticket is related to a project,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   140
we should provide all the non published versions of this project
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   141
(`Version` and `Project` can be related through the `version_of`
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   142
relation). Conversely, if we do not know yet the project, it would not
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   143
make sense to propose all existing versions as it could potentially
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   144
lead to incoherences. Even if these will be caught by some
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   145
RQLConstraint, it is wise not to tempt the user with error-inducing
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   146
candidate values.
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   147
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   148
The "ticket is related to a project" part must be decomposed as:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   149
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   150
* this is a new ticket which is created is the context of a project
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   151
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   152
* this is an already existing ticket, linked to a project (through the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   153
  `concerns` relation)
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   154
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   155
* there is no related project (quite unlikely given the cardinality of
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   156
  the `concerns` relation, so it can only mean that we are creating a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   157
  new ticket, and a project is about to be selected but there is no
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   158
  ``__linkto`` argument)
4443
83ff2bb898a5 start documenting forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2172
diff changeset
   159
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   160
.. note::
1714
a721966779be new book layout, do not compile yet
sylvain.thenault@logilab.fr
parents:
diff changeset
   161
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   162
   the last situation could happen in several ways, but of course in a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   163
   polished application, the paths to ticket creation should be
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   164
   controlled so as to avoid a suboptimal end-user experience
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   165
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   166
Hence, we try to fetch the related project.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   167
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   168
.. sourcecode:: python
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   169
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   170
        veid = None
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   171
        if not entity.has_eid():
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   172
            peids = entity.linked_to('concerns', 'subject')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   173
            peid = peids and peids[0]
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   174
        else:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   175
            peid = entity.project.eid
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   176
            veid = entity.done_in and entity.done_in[0].eid
4743
026a89520184 [book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4465
diff changeset
   177
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   178
We distinguish between entity creation and entity modification using
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   179
the ``Entity.has_eid()`` method, which returns `False` on creation. At
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   180
creation time the only way to get a project is through the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   181
``__linkto`` parameter. Notice that we fetch the version in which the
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   182
ticket is `done_in` if any, for later.
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   183
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   184
.. note::
4743
026a89520184 [book] a few autoclasses for renderers, misc tweaks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4465
diff changeset
   185
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   186
  the implementation above assumes that if there is a ``__linkto``
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   187
  parameter, it is only about a project. While it makes sense most of
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   188
  the time, it is not an absolute. Depending on how an entity creation
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   189
  action action url is built, several outcomes could be possible
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   190
  there
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   191
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   192
If the ticket is already linked to a project, fetching it is
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   193
trivial. Then we add the relevant version to the initial vocabulary.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   194
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   195
.. sourcecode:: python
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   196
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   197
        if peid:
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   198
            rschema = form._cw.vreg.schema['done_in'].rdef('Ticket', 'Version')
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   199
            rset = form._cw.execute(
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   200
                'Any V, VN ORDERBY version_sort_value(VN) '
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   201
                'WHERE V version_of P, P eid %(p)s, V num VN, '
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   202
                'V in_state ST, NOT ST name "published"', {'p': peid})
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   203
            vocab += [(v.view('combobox'), v.eid) for v in rset.entities()
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   204
                      if rschema.has_perm(form._cw, 'add', toeid=v.eid)
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   205
                      and v.eid != veid]
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   206
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   207
.. warning::
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   208
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   209
   we have to defend ourselves against lack of a project eid. Given
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   210
   the cardinality of the `concerns` relation, there *must* be a
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   211
   project, but this rule can only be enforced at validation time,
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   212
   which will happen of course only after form subsmission
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   213
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   214
Here, given a project eid, we complete the vocabulary with all
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   215
unpublished versions defined in the project (sorted by number) for
5465
a838ac0ff890 [doc/book] not anymore ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5464
diff changeset
   216
which the current user is allowed to establish the relation.
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   217
7643
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   218
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   219
Building self-posted form with custom fields/widgets
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   220
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   221
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   222
Sometimes you want a form that is not related to entity edition. For those,
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   223
you'll have to handle form posting by yourself. Here is a complete example on how
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   224
to achieve this (and more).
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   225
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   226
Imagine you want a form that selects a month period. There are no proper
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   227
field/widget to handle this in CubicWeb, so let's start by defining them:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   228
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   229
.. sourcecode:: python
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   230
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   231
    # let's have the whole import list at the beginning, even those necessary for
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   232
    # subsequent snippets
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   233
    from logilab.common import date
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   234
    from logilab.mtconverter import xml_escape
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   235
    from cubicweb.view import View
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   236
    from cubicweb.selectors import match_kwargs
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   237
    from cubicweb.web import RequestError, ProcessFormError
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   238
    from cubicweb.web import formfields as fields, formwidgets as wdgs
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   239
    from cubicweb.web.views import forms, calendar
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   240
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   241
    class MonthSelect(wdgs.Select):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   242
        """Custom widget to display month and year. Expect value to be given as a
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   243
        date instance.
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   244
        """
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   245
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   246
        def format_value(self, form, field, value):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   247
            return u'%s/%s' % (value.year, value.month)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   248
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   249
        def process_field_data(self, form, field):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   250
            val = super(MonthSelect, self).process_field_data(form, field)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   251
            try:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   252
                year, month = val.split('/')
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   253
                year = int(year)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   254
                month = int(month)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   255
                return date.date(year, month, 1)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   256
            except ValueError:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   257
                raise ProcessFormError(
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   258
                    form._cw._('badly formated date string %s') % val)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   259
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   260
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   261
    class MonthPeriodField(fields.CompoundField):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   262
        """custom field composed of two subfields, 'begin_month' and 'end_month'.
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   263
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   264
        It expects to be used on form that has 'mindate' and 'maxdate' in its
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   265
        extra arguments, telling the range of month to display.
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   266
        """
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   267
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   268
        def __init__(self, *args, **kwargs):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   269
            kwargs.setdefault('widget', wdgs.IntervalWidget())
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   270
            super(MonthPeriodField, self).__init__(
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   271
                [fields.StringField(name='begin_month',
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   272
                                    choices=self.get_range, sort=False,
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   273
                                    value=self.get_mindate,
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   274
                                    widget=MonthSelect()),
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   275
                 fields.StringField(name='end_month',
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   276
                                    choices=self.get_range, sort=False,
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   277
                                    value=self.get_maxdate,
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   278
                                    widget=MonthSelect())], *args, **kwargs)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   279
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   280
        @staticmethod
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   281
        def get_range(form, field):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   282
            mindate = date.todate(form.cw_extra_kwargs['mindate'])
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   283
            maxdate = date.todate(form.cw_extra_kwargs['maxdate'])
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   284
            assert mindate <= maxdate
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   285
            _ = form._cw._
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   286
            months = []
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   287
            while mindate <= maxdate:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   288
                label = '%s %s' % (_(calendar.MONTHNAMES[mindate.month - 1]),
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   289
                                   mindate.year)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   290
                value = field.widget.format_value(form, field, mindate)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   291
                months.append( (label, value) )
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   292
                mindate = date.next_month(mindate)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   293
            return months
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   294
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   295
        @staticmethod
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   296
        def get_mindate(form, field):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   297
            return form.cw_extra_kwargs['mindate']
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   298
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   299
        @staticmethod
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   300
        def get_maxdate(form, field):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   301
            return form.cw_extra_kwargs['maxdate']
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   302
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   303
        def process_posted(self, form):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   304
            for field, value in super(MonthPeriodField, self).process_posted(form):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   305
                if field.name == 'end_month':
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   306
                    value = date.last_day(value)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   307
                yield field, value
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   308
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   309
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   310
Here we first define a widget that will be used to select the beginning and the
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   311
end of the period, displaying months like '<month> YYYY' but using 'YYYY/mm' as
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   312
actual value.
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   313
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   314
We then define a field that will actually hold two fields, one for the beginning
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   315
and another for the end of the period. Each subfield uses the widget we defined
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   316
earlier, and the outer field itself uses the standard
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   317
:class:`IntervalWidget`. The field adds some logic:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   318
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   319
* a vocabulary generation function `get_range`, used to populate each sub-field
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   320
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   321
* two 'value' functions `get_mindate` and `get_maxdate`, used to tell to
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   322
  subfields which value they should consider on form initialization
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   323
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   324
* overriding of `process_posted`, called when the form is being posted, so that
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   325
  the end of the period is properly set to the last day of the month.
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   326
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   327
Now, we can define a very simple form:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   328
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   329
.. sourcecode:: python
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   330
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   331
    class MonthPeriodSelectorForm(forms.FieldsForm):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   332
        __regid__ = 'myform'
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   333
        __select__ = match_kwargs('mindate', 'maxdate')
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   334
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   335
        form_buttons = [wdgs.SubmitButton()]
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   336
        form_renderer_id = 'onerowtable'
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   337
        period = MonthPeriodField()
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   338
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   339
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   340
where we simply add our field, set a submit button and use a very simple renderer
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   341
(try others!). Also we specify a selector that ensures form will have arguments
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   342
necessary to our field.
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   343
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   344
Now, we need a view that will wrap the form and handle post when it occurs,
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   345
simply displaying posted values in the page:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   346
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   347
.. sourcecode:: python
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   348
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   349
    class SelfPostingForm(View):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   350
        __regid__ = 'myformview'
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   351
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   352
        def call(self):
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   353
            mindate, maxdate = date.date(2010, 1, 1), date.date(2012, 1, 1)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   354
            form = self._cw.vreg['forms'].select(
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   355
                'myform', self._cw, mindate=mindate, maxdate=maxdate, action='')
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   356
            try:
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   357
                posted = form.process_posted()
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   358
                self.w(u'<p>posted values %s</p>' % xml_escape(repr(posted)))
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   359
            except RequestError: # no specified period asked
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   360
                pass
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   361
            form.render(w=self.w, formvalues=self._cw.form)
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   362
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   363
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   364
Notice usage of the :meth:`process_posted` method, that will return a dictionary
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   365
of typed values (because they have been processed by the field). In our case, when
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   366
the form is posted you should see a dictionary with 'begin_month' and 'end_month'
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   367
as keys with the selected dates as value (as a python `date` object).
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   368
f3e3892fc7e3 [book, form] include complete example of self-posted form with custom field/widget + minor other changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5869
diff changeset
   369
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   370
APIs
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   371
~~~~
5418
4f0047cfecb5 [doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5400
diff changeset
   372
4f0047cfecb5 [doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5400
diff changeset
   373
.. automodule:: cubicweb.web.formfields
4f0047cfecb5 [doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5400
diff changeset
   374
.. automodule:: cubicweb.web.formwidgets
4f0047cfecb5 [doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5400
diff changeset
   375
.. automodule:: cubicweb.web.views.forms
4f0047cfecb5 [doc] reorganize for chapter structure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5400
diff changeset
   376
.. automodule:: cubicweb.web.views.formrenderers
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5350
diff changeset
   377
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5418
diff changeset
   378