web/views/forms.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 23 Sep 2013 14:55:56 +0200
changeset 9873 1257e909d25e
parent 9770 112c884b2d8d
child 9892 928732ec00dd
permissions -rw-r--r--
[forms] consider inline creation form information as linkto info. Closes #3161121
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
     1
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5368
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    18
"""
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    19
Base form classes
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    20
-----------------
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    21
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    22
.. Note:
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    23
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    24
   Form is the glue that bind a context to a set of fields, and is rendered
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    25
   using a form renderer. No display is actually done here, though you'll find
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    26
   some attributes of form that are used to control the rendering process.
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
5464
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5424
diff changeset
    28
Besides the automagic form we'll see later, there are roughly two main
c6c9a80ad1dd [doc/book] dissection of a form chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5424
diff changeset
    29
form classes in |cubicweb|:
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    30
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    31
.. autoclass:: cubicweb.web.views.forms.FieldsForm
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    32
.. autoclass:: cubicweb.web.views.forms.EntityFieldsForm
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    33
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    34
As you have probably guessed, choosing between them is easy. Simply ask you the
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    35
question 'I am editing an entity or not?'. If the answer is yes, use
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    36
:class:`EntityFieldsForm`, else use :class:`FieldsForm`.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    37
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    38
Actually there exists a third form class:
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    39
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    40
.. autoclass:: cubicweb.web.views.forms.CompositeForm
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    41
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    42
but you'll use this one rarely.
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
"""
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
    44
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
__docformat__ = "restructuredtext en"
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
from warnings import warn
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
    49
from logilab.common import dictattr, tempattr
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
    50
from logilab.common.decorators import iclassmethod, cached
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
from logilab.common.compat import any
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
    52
from logilab.common.textutils import splitstrip
3953
19aefd78f61b [forms] deprecate form_render(**kwargs) in favor of render(formvalues=None, rendervalues=None, renderer=None)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3923
diff changeset
    53
from logilab.common.deprecation import deprecated
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
    55
from cubicweb import ValidationError, neg_role
6584
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
    56
from cubicweb.utils import support_args
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
    57
from cubicweb.predicates import non_final_entity, match_kwargs, one_line_rset
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
    58
from cubicweb.web import RequestError, ProcessFormError
8665
e65af61bde7d [uicfg] uicfg.py moves from web/ to web/views/ (prepares #2406609)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8190
diff changeset
    59
from cubicweb.web import form, formwidgets as fwdgs
e65af61bde7d [uicfg] uicfg.py moves from web/ to web/views/ (prepares #2406609)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8190
diff changeset
    60
from cubicweb.web.views import uicfg
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
    61
from cubicweb.web.formfields import guess_field
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
class FieldsForm(form.Form):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    65
    """This is the base class for fields based forms.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    66
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    67
    **Attributes**
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    68
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    69
    The following attributes may be either set on subclasses or given on
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    70
    form selection to customize the generated form:
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    71
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    72
    :attr:`needs_js`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    73
      sequence of javascript files that should be added to handle this form
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    74
      (through :meth:`~cubicweb.web.request.Request.add_js`)
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    75
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    76
    :attr:`needs_css`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    77
      sequence of css files that should be added to handle this form (through
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    78
      :meth:`~cubicweb.web.request.Request.add_css`)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    79
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    80
    :attr:`domid`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    81
      value for the "id" attribute of the <form> tag
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    82
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    83
    :attr:`action`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    84
      value for the "action" attribute of the <form> tag
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    85
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    86
    :attr:`onsubmit`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    87
      value for the "onsubmit" attribute of the <form> tag
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    88
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    89
    :attr:`cssclass`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    90
      value for the "class" attribute of the <form> tag
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    91
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    92
    :attr:`cssstyle`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    93
      value for the "style" attribute of the <form> tag
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
    94
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    95
    :attr:`cwtarget`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    96
      value for the "cubicweb:target" attribute of the <form> tag
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    97
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    98
    :attr:`redirect_path`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    99
      relative to redirect to after submitting the form
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   100
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   101
    :attr:`copy_nav_params`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   102
      flag telling if navigation parameters should be copied back in hidden
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   103
      inputs
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   104
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   105
    :attr:`form_buttons`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   106
      sequence of form control (:class:`~cubicweb.web.formwidgets.Button`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   107
      widgets instances)
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   108
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   109
    :attr:`form_renderer_id`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   110
      identifier of the form renderer to use to render the form
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   111
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   112
    :attr:`fieldsets_in_order`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   113
      sequence of fieldset names , to control order
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   114
9717
a6834e2dcc1b [forms] Add autocomplete attribute for formrenderers
Alain Begey <alain@unlish.com>
parents: 8900
diff changeset
   115
    :attr:`autocomplete`
a6834e2dcc1b [forms] Add autocomplete attribute for formrenderers
Alain Begey <alain@unlish.com>
parents: 8900
diff changeset
   116
      set to False to add 'autocomplete=off' in the form open tag
a6834e2dcc1b [forms] Add autocomplete attribute for formrenderers
Alain Begey <alain@unlish.com>
parents: 8900
diff changeset
   117
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   118
    **Generic methods**
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   119
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   120
    .. automethod:: cubicweb.web.form.Form.field_by_name(name, role=None)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   121
    .. automethod:: cubicweb.web.form.Form.fields_by_name(name, role=None)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   122
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   123
    **Form construction methods**
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   124
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   125
    .. automethod:: cubicweb.web.form.Form.remove_field(field)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   126
    .. automethod:: cubicweb.web.form.Form.append_field(field)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   127
    .. automethod:: cubicweb.web.form.Form.insert_field_before(field, name, role=None)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   128
    .. automethod:: cubicweb.web.form.Form.insert_field_after(field, name, role=None)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   129
    .. automethod:: cubicweb.web.form.Form.add_hidden(name, value=None, **kwargs)
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   130
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   131
    **Form rendering methods**
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   132
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   133
    .. automethod:: cubicweb.web.views.forms.FieldsForm.render
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   134
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   135
    **Form posting methods**
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   136
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   137
    Once a form is posted, you can retrieve the form on the controller side and
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   138
    use the following methods to ease processing. For "simple" forms, this
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   139
    should looks like :
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   140
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   141
    .. sourcecode :: python
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   142
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   143
        form = self._cw.vreg['forms'].select('myformid', self._cw)
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   144
        posted = form.process_posted()
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   145
        # do something with the returned dictionary
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   146
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   147
    Notice that form related to entity edition should usually use the
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   148
    `edit` controller which will handle all the logic for you.
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   149
7632
3c9dfc6e820b [book] fix some rest/sphinx errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7586
diff changeset
   150
    .. automethod:: cubicweb.web.views.forms.FieldsForm.process_posted
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   151
    .. automethod:: cubicweb.web.views.forms.FieldsForm.iter_modified_fields
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   152
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   153
    __regid__ = 'base'
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   154
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   156
    # attributes overrideable by subclasses or through __init__
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
    needs_js = ('cubicweb.ajax.js', 'cubicweb.edition.js',)
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
    needs_css = ('cubicweb.form.css',)
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
    action = None
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
    cssclass = None
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
    cssstyle = None
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
    cwtarget = None
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   163
    redirect_path = None
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   164
    form_buttons = None
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
    form_renderer_id = 'default'
2573
9c414dbc76da add default values for fielsets_in_order, document base form attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   166
    fieldsets_in_order = None
9717
a6834e2dcc1b [forms] Add autocomplete attribute for formrenderers
Alain Begey <alain@unlish.com>
parents: 8900
diff changeset
   167
    autocomplete = True
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   169
    @property
4164
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   170
    def needs_multipart(self):
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
        """true if the form needs enctype=multipart/form-data"""
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   172
        return any(field.needs_multipart for field in self.fields)
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
7582
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   174
    def _get_onsubmit(self):
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   175
        try:
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   176
            return self._onsubmit
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   177
        except AttributeError:
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   178
            return "return freezeFormButtons('%(domid)s');" % dictattr(self)
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   179
    def _set_onsubmit(self, value):
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   180
        self._onsubmit = value
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   181
    onsubmit = property(_get_onsubmit, _set_onsubmit)
dd4487c3f21e [form, renderer] closes #1787234: [form] onsubmit interpolated by the form renderer, it shouldn't
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6906
diff changeset
   182
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   183
    def add_media(self):
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   184
        """adds media (CSS & JS) required by this widget"""
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   185
        if self.needs_js:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   186
            self._cw.add_js(self.needs_js)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   187
        if self.needs_css:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   188
            self._cw.add_css(self.needs_css)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   189
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7929
diff changeset
   190
    def render(self, formvalues=None, renderer=None, **kwargs):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   191
        """Render this form, using the `renderer` given as argument or the
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   192
        default according to :attr:`form_renderer_id`. The rendered form is
9701
46c8d8701240 an unicode string -> a unicode string
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8900
diff changeset
   193
        returned as a unicode string.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   194
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   195
        `formvalues` is an optional dictionary containing values that will be
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   196
        considered as field's value.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   197
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   198
        Extra keyword arguments will be given to renderer's :meth:`render` method.
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   199
        """
6453
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   200
        w = kwargs.pop('w', None)
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   201
        if w is None:
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   202
            warn('[3.10] you should specify "w" to form.render() named arguments',
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   203
                 DeprecationWarning, stacklevel=2)
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   204
            data = []
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   205
            w = data.append
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   206
        else:
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   207
            data = None
4167
73e649a7797d fix render (formerly form_render) prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4164
diff changeset
   208
        self.build_context(formvalues)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   209
        if renderer is None:
4164
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   210
            renderer = self.default_renderer()
6584
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
   211
        if support_args(renderer.render, 'w'):
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
   212
            renderer.render(w, self, kwargs)
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
   213
        else:
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
   214
            warn('[3.10] you should add "w" as first argument o %s.render()'
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
   215
                 % renderer.__class__, DeprecationWarning)
c4aa6186d3a3 [3.10] renderer.render prototype bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6453
diff changeset
   216
            w(renderer.render(self, kwargs))
6453
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   217
        if data is not None:
7fdd780d87e4 [form] unify form.render prototype to take a 'w' argument as other view/components render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6246
diff changeset
   218
            return '\n'.join(data)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   219
4164
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   220
    def default_renderer(self):
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   221
        return self._cw.vreg['formrenderers'].select(
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   222
            self.form_renderer_id, self._cw,
4697
b8263d717e74 [web] fix muledit rendering bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4668
diff changeset
   223
            rset=self.cw_rset, row=self.cw_row, col=self.cw_col or 0)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   224
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   225
    formvalues = None
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   226
    def build_context(self, formvalues=None):
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   227
        """build form context values (the .context attribute which is a
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   228
        dictionary with field instance as key associated to a dictionary
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   229
        containing field 'name' (qualified), 'id', 'value' (for display, always
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   230
        a string).
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   231
        """
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   232
        if self.formvalues is not None:
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   233
            return # already built
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   234
        self.formvalues = formvalues or {}
6246
62e25fac41cd [views/reledit] refactor composite handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5656
diff changeset
   235
        # use a copy in case fields are modified while context is built (eg
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   236
        # __linkto handling for instance)
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   237
        for field in self.fields[:]:
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   238
            for field in field.actual_fields(self):
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   239
                field.form_init(self)
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   240
        # store used field in an hidden input for later usage by a controller
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   241
        fields = set()
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   242
        eidfields = set()
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   243
        for field in self.fields:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   244
            if field.eidparam:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   245
                eidfields.add(field.role_name())
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   246
            elif field.name not in self.control_fields:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   247
                fields.add(field.role_name())
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   248
        if fields:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   249
            self.add_hidden('_cw_fields', u','.join(fields))
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   250
        if eidfields:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   251
            self.add_hidden('_cw_entity_fields', u','.join(eidfields),
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   252
                            eidparam=True)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   253
5588
377c9adfe81e [forms] refactor action handling to ease overriding while keeping action overrideable by instance (closes #969167)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5547
diff changeset
   254
    _default_form_action_path = 'edit'
377c9adfe81e [forms] refactor action handling to ease overriding while keeping action overrideable by instance (closes #969167)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5547
diff changeset
   255
    def form_action(self):
5656
abe97430b3f5 [form] avoid spurious warning subsequent for form's action refactoring w/ autoforms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5598
diff changeset
   256
        try:
abe97430b3f5 [form] avoid spurious warning subsequent for form's action refactoring w/ autoforms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5598
diff changeset
   257
            action = self.get_action() # avoid spurious warning w/ autoform bw compat property
abe97430b3f5 [form] avoid spurious warning subsequent for form's action refactoring w/ autoforms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5598
diff changeset
   258
        except AttributeError:
abe97430b3f5 [form] avoid spurious warning subsequent for form's action refactoring w/ autoforms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5598
diff changeset
   259
            action = self.action
abe97430b3f5 [form] avoid spurious warning subsequent for form's action refactoring w/ autoforms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5598
diff changeset
   260
        if action is None:
5598
0a68e7f5829c [form] missing return...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5588
diff changeset
   261
            return self._cw.build_url(self._default_form_action_path)
5656
abe97430b3f5 [form] avoid spurious warning subsequent for form's action refactoring w/ autoforms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5598
diff changeset
   262
        return action
5588
377c9adfe81e [forms] refactor action handling to ease overriding while keeping action overrideable by instance (closes #969167)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5547
diff changeset
   263
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   264
    # controller form processing methods #######################################
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   265
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   266
    def iter_modified_fields(self, editedfields=None, entity=None):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   267
        """return a generator on field that has been modified by the posted
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   268
        form.
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   269
        """
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   270
        if editedfields is None:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   271
            try:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   272
                editedfields = self._cw.form['_cw_fields']
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   273
            except KeyError:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   274
                raise RequestError(self._cw._('no edited fields specified'))
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   275
        entityform = entity and self.field_by_name.im_func.func_code.co_argcount == 4 # XXX
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   276
        for editedfield in splitstrip(editedfields):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   277
            try:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   278
                name, role = editedfield.split('-')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7728
diff changeset
   279
            except Exception:
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   280
                name = editedfield
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   281
                role = None
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   282
            if entityform:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   283
                field = self.field_by_name(name, role, eschema=entity.e_schema)
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   284
            else:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   285
                field = self.field_by_name(name, role)
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   286
            if field.has_been_modified(self):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   287
                yield field
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   288
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   289
    def process_posted(self):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   290
        """use this method to process the content posted by a simple form.  it
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   291
        will return a dictionary with field names as key and typed value as
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   292
        associated value.
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   293
        """
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   294
        with tempattr(self, 'formvalues', {}): # init fields value cache
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   295
            errors = []
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   296
            processed = {}
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   297
            for field in self.iter_modified_fields():
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   298
                try:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   299
                    for field, value in field.process_posted(self):
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   300
                        processed[field.role_name()] = value
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   301
                except ProcessFormError as exc:
7584
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   302
                    errors.append((field, exc))
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   303
            if errors:
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   304
                errors = dict((f.role_name(), unicode(ex)) for f, ex in errors)
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   305
                raise ValidationError(None, errors)
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   306
            return processed
e1881933f366 [form, controller] closes #1787233: form should provide a method to process posted content
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7583
diff changeset
   307
4660
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4659
diff changeset
   308
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   309
class EntityFieldsForm(FieldsForm):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   310
    """This class is designed for forms used to edit some entities. It should
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   311
    handle for you all the underlying stuff necessary to properly work with the
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   312
    generic :class:`~cubicweb.web.views.editcontroller.EditController`.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   313
    """
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   314
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   315
    __regid__ = 'base'
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   316
    __select__ = (match_kwargs('entity')
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   317
                  | (one_line_rset() & non_final_entity()))
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   318
    domid = 'entityForm'
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   319
    uicfg_aff = uicfg.autoform_field
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   320
    uicfg_affk = uicfg.autoform_field_kwargs
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   321
4257
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   322
    @iclassmethod
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   323
    def field_by_name(cls_or_self, name, role=None, eschema=None):
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   324
        """return field with the given name and role. If field is not explicitly
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   325
        defined for the form but `eclass` is specified, guess_field will be
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   326
        called.
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   327
        """
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   328
        try:
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   329
            return super(EntityFieldsForm, cls_or_self).field_by_name(name, role)
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   330
        except form.FieldNotFound:
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   331
            if eschema is None or role is None or not name in eschema.schema:
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   332
                raise
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   333
            rschema = eschema.schema.rschema(name)
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   334
            # XXX use a sample target type. Document this.
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   335
            tschemas = rschema.targets(eschema, role)
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   336
            fieldcls = cls_or_self.uicfg_aff.etype_get(
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   337
                eschema, rschema, role, tschemas[0])
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   338
            kwargs = cls_or_self.uicfg_affk.etype_get(
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   339
                eschema, rschema, role, tschemas[0])
4257
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   340
            if kwargs is None:
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   341
                kwargs = {}
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   342
            if fieldcls:
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   343
                if not isinstance(fieldcls, type):
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   344
                    return fieldcls # already and instance
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   345
                return fieldcls(name=name, role=role, eidparam=True, **kwargs)
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   346
            if isinstance(cls_or_self, type):
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   347
                req = None
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   348
            else:
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   349
                req = cls_or_self._cw
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   350
            field = guess_field(eschema, rschema, role, req=req, eidparam=True, **kwargs)
4257
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   351
            if field is None:
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   352
                raise
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   353
            return field
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   354
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   355
    def __init__(self, _cw, rset=None, row=None, col=None, **kwargs):
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   356
        try:
4206
cebdf8ee5ad7 [forms] edited_entity must be set before calling session_key()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4134
diff changeset
   357
            self.edited_entity = kwargs.pop('entity')
4257
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   358
        except KeyError:
4252
6c4f109c2b03 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4248 4212
diff changeset
   359
            self.edited_entity = rset.complete_entity(row or 0, col or 0)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   360
        msg = kwargs.pop('submitmsg', None)
4257
0a9b38a492e1 fix bad merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   361
        super(EntityFieldsForm, self).__init__(_cw, rset, row, col, **kwargs)
8666
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   362
        self.uicfg_aff = self._cw.vreg['uicfg'].select(
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   363
            'autoform_field', self._cw, entity=self.edited_entity)
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   364
        self.uicfg_affk = self._cw.vreg['uicfg'].select(
1dd655788ece make ui configurations selectable (closes #2406609)
Florent Cayré <florent.cayre@logilab.fr>
parents: 8665
diff changeset
   365
            'autoform_field_kwargs', self._cw, entity=self.edited_entity)
8900
010a59e12d89 use cw_etype instead of __regid__
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8748
diff changeset
   366
        self.add_hidden('__type', self.edited_entity.cw_etype, eidparam=True)
4659
f8326ff98f37 [form] complete 25de2eb0432b by ignore req parameters by default for all hidden fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   367
        self.add_hidden('eid', self.edited_entity.eid)
6906
5f13aefb470b [forms] EntityFieldsForm now take extra mainentity argument, for usage with composite form (the main form) where one of the subform edits the main entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6584
diff changeset
   368
        # mainform default to true in parent, hence default to True
5f13aefb470b [forms] EntityFieldsForm now take extra mainentity argument, for usage with composite form (the main form) where one of the subform edits the main entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6584
diff changeset
   369
        if kwargs.get('mainform', True) or kwargs.get('mainentity', False):
4164
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   370
            self.add_hidden(u'__maineid', self.edited_entity.eid)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   371
            # If we need to directly attach the new object to another one
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   372
            if '__linkto' in self._cw.form:
2050
ce184fdb1e56 fix submit message handling in entity form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   373
                if msg:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   374
                    msg = '%s %s' % (msg, self._cw._('and linked'))
2050
ce184fdb1e56 fix submit message handling in entity form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   375
                else:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   376
                    msg = self._cw._('entity linked')
2050
ce184fdb1e56 fix submit message handling in entity form
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2049
diff changeset
   377
        if msg:
7728
0fa5ba0229cd [deprecation] __message hidden generate deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7632
diff changeset
   378
            msgid = self._cw.set_redirect_message(msg)
0fa5ba0229cd [deprecation] __message hidden generate deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7632
diff changeset
   379
            self.add_hidden('_cwmsgid', msgid)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   380
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   381
    def add_linkto_hidden(self):
7889
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   382
        """add the __linkto hidden field used to directly attach the new object
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   383
        to an existing other one when the relation between those two is not
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   384
        already present in the form.
7889
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   385
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   386
        Warning: this method must be called only when all form fields are setup
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   387
        """
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   388
        for (rtype, role), eids in self.linked_to.iteritems():
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   389
            # if the relation is already setup by a form field, do not add it
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   390
            # in a __linkto hidden to avoid setting it twice in the controller
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   391
            try:
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   392
                self.field_by_name(rtype, role)
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   393
            except form.FieldNotFound:
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   394
                for eid in eids:
7929
900f1627b171 [forms] fix bug in linkto refactoring (misordered arguments)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7889
diff changeset
   395
                    self.add_hidden('__linkto', '%s:%s:%s' % (rtype, eid, role))
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   396
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   397
    def render(self, *args, **kwargs):
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   398
        self.add_linkto_hidden()
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   399
        return super(EntityFieldsForm, self).render(*args, **kwargs)
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   400
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   401
    @property
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   402
    @cached
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   403
    def linked_to(self):
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   404
        linked_to = {}
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   405
        # case where this is an embeded creation form
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   406
        try:
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   407
            eid = int(self.cw_extra_kwargs['peid'])
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   408
        except KeyError:
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   409
            pass
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   410
        else:
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   411
            ltrtype = self.cw_extra_kwargs['rtype']
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   412
            ltrole = neg_role(self.cw_extra_kwargs['role'])
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   413
            linked_to[(ltrtype, ltrole)] = [eid]
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   414
        # now consider __linkto if the current form is the main form
7889
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   415
        try:
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   416
            self.field_by_name('__maineid')
6cebeb1f386a [linkto] test for main form should be done in the link_to dictionary computation, else we may get erroneous values for sub-forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7875
diff changeset
   417
        except form.FieldNotFound:
9873
1257e909d25e [forms] consider inline creation form information as linkto info. Closes #3161121
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   418
            return linked_to
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   419
        for linkto in self._cw.list_form_param('__linkto'):
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   420
            ltrtype, eid, ltrole = linkto.split(':')
8748
f5027f8d2478 drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   421
            linked_to.setdefault((ltrtype, ltrole), []).append(int(eid))
7875
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   422
        return linked_to
65e460690139 [form, entity] refactor '__linkto', now handled by the entity form, not the entity itself. Closes #1931543
Florent Cayré <florent.cayre@gmail.com>
parents: 7815
diff changeset
   423
3922
69020a7c234a refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3880
diff changeset
   424
    def session_key(self):
69020a7c234a refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3880
diff changeset
   425
        """return the key that may be used to store / retreive data about a
69020a7c234a refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3880
diff changeset
   426
        previous post which failed because of a validation error
69020a7c234a refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3880
diff changeset
   427
        """
4133
24ffe983abfc force_session_key default to None in base form class...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3953
diff changeset
   428
        if self.force_session_key is not None:
3922
69020a7c234a refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3880
diff changeset
   429
            return self.force_session_key
4133
24ffe983abfc force_session_key default to None in base form class...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3953
diff changeset
   430
        # XXX if this is a json request, suppose we should redirect to the
24ffe983abfc force_session_key default to None in base form class...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3953
diff changeset
   431
        # entity primary view
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   432
        if self._cw.ajax_request and self.edited_entity.has_eid():
4133
24ffe983abfc force_session_key default to None in base form class...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3953
diff changeset
   433
            return '%s#%s' % (self.edited_entity.absolute_url(), self.domid)
4662
79c0788ba7f6 add XXX note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4660
diff changeset
   434
        # XXX we should not consider some url parameters that may lead to
79c0788ba7f6 add XXX note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4660
diff changeset
   435
        # different url after a validation error
4183
b5aa030bb2f9 use ._cw instead of .req (reintroduced by merge of stable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4181
diff changeset
   436
        return '%s#%s' % (self._cw.url(), self.domid)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   437
4164
119a374c5eb4 form_add_hidden -> add_hidden
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4162
diff changeset
   438
    def default_renderer(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   439
        return self._cw.vreg['formrenderers'].select(
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   440
            self.form_renderer_id, self._cw, rset=self.cw_rset, row=self.cw_row,
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3396
diff changeset
   441
            col=self.cw_col, entity=self.edited_entity)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   442
4583
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4284
diff changeset
   443
    def should_display_add_new_relation_link(self, rschema, existant, card):
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4284
diff changeset
   444
        return False
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4284
diff changeset
   445
4167
73e649a7797d fix render (formerly form_render) prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4164
diff changeset
   446
    # controller side method (eg POST reception handling)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   447
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   448
    def actual_eid(self, eid):
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   449
        # should be either an int (existant entity) or a variable (to be
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   450
        # created entity)
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   451
        assert eid or eid == 0, repr(eid) # 0 is a valid eid
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   452
        try:
8748
f5027f8d2478 drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   453
            return int(eid)
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   454
        except ValueError:
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   455
            try:
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   456
                return self._cw.data['eidmap'][eid]
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   457
            except KeyError:
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   458
                self._cw.data['eidmap'][eid] = None
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   459
                return None
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   460
3476
6e927b729ae1 [uicfg, autoform] more consistent/powerful autoform_section rtags by using formtype/section; deprecates autoform_is_inlined; refactor automatci form and renderer thanks to this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3467
diff changeset
   461
    def editable_relations(self):
2089
b1070848726b backport default branch fix 4ec37d33657e (EntityFieldsForm methods implementation)
Florent <florent@secondweb.fr>
parents: 2080
diff changeset
   462
        return ()
b1070848726b backport default branch fix 4ec37d33657e (EntityFieldsForm methods implementation)
Florent <florent@secondweb.fr>
parents: 2080
diff changeset
   463
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   464
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   465
class CompositeFormMixIn(object):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
   466
    __regid__ = 'composite'
3467
a6405235aac6 [tests] make unittest_views_editforms pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3451
diff changeset
   467
    form_renderer_id = __regid__
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   468
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   469
    def __init__(self, *args, **kwargs):
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   470
        super(CompositeFormMixIn, self).__init__(*args, **kwargs)
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   471
        self.forms = []
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   472
3513
c002f6488631 [form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3512
diff changeset
   473
    def add_subform(self, subform):
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   474
        """mark given form as a subform and append it"""
3513
c002f6488631 [form] replace is_subform by parent_form, carrying more information at the same cost
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3512
diff changeset
   475
        subform.parent_form = self
2005
e8032965f37a turn every form class into appobject. They should not be instantiated manually anymore.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   476
        self.forms.append(subform)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   477
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   478
    def build_context(self, formvalues=None):
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   479
        super(CompositeFormMixIn, self).build_context(formvalues)
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   480
        for form in self.forms:
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   481
            form.build_context(formvalues)
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   482
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   483
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   484
class CompositeForm(CompositeFormMixIn, FieldsForm):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   485
    """Form composed of sub-forms. Typical usage is edition of multiple entities
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   486
    at once.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   487
    """
2920
64322aa83a1d start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   488
3510
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   489
class CompositeEntityForm(CompositeFormMixIn, EntityFieldsForm):
bf746bf4a394 rename form_build_context to build_context, and call it from form, not renderer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3354
diff changeset
   490
    pass # XXX why is this class necessary?