zc.form


Namezc.form JSON
Version 2.0 PyPI version JSON
download
home_pagehttps://github.com/zopefoundation/zc.form
SummaryExtra browser widgets and alternative approaches for zope.formlib.
upload_time2023-02-06 14:06:29
maintainer
docs_urlNone
authorZope Foundation and Contributors
requires_python>=3.7
licenseZPL 2.1
keywords zope formlib form widget extra
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            The zc.form package is a possibly temporary appendage used to hold extra
browser widgets and alternative approaches to code found in the
zope.formlib package.  Most or all of the code is created by Zope
Corporation and is intended for eventual folding into the main Zope 3
release.


.. contents::

=======
Changes
=======

2.0 (2023-02-06)
----------------

- Add support for Python 3.8, 3.9, 3.10, 3.11.

- Drop support for Python 2.7, 3.5, 3.6.


1.1 (2019-02-11)
----------------

- Fix ZCML configuration issue if the ``[mruwidget]`` extra was not installed.


1.0 (2019-01-11)
----------------

Features
++++++++

- Claim support for Python 3.5, 3.6, 3.7, PyPy and PyPy3.

Bugfixes
++++++++

- Fix a ``NameError`` in ``BaseVocabularyDisplay.render()``.

- Actually pass a ``missing_value`` set on the ``Combination`` field to the
  containing fields.

Caveats
+++++++

- Installation of ``MruSourceInputWidget`` and ``TimeZoneWidget`` requires the
  ``[mruwidget]`` extra to break dependency on ``zc.resourcelibrary`` for
  projects which do not need it.


0.5 (2016-08-02)
----------------

- Bind fields that are contained in a ``zc.form.field.Combination`` to fix the
  ``context`` of those fields.


0.4 (2016-01-12)
----------------

- Get rid of the `zope.app.pagetemplate` dependency.


0.3 (2014-04-23)
----------------

- Remove requirement, that ``zc.form.field.Combination`` needs at least
  two subfields.


0.2 (2011-09-24)
----------------

- Got rid of ``zope.app.form`` dependency by requiring at least
  ``zope.formlib`` 4.0.

- Got rid of ``zope.app.component`` dependency by requiring at least
  ``zope.component`` 3.8.

- Depending on ``zope.catalog`` instead of ``zope.app.catalog``.

- Depending on ``zope.security`` instead of ``zope.app.security``.

- Depending on ``zope.app.wsgi`` >=3.7 instead of ``zope.app.testing`` for
  test setup.

- Depending on ``zope.browserpage`` and ``zope.container`` instead of
  ``zope.app.publisher``.

- Got rid of the following dependencies:

  - ``zope.app.basicskin``
  - ``zope.app.securitypolicy``
  - ``zope.app.zapi``
  - ``zope.app.zcmlfiles``

- Fixed tests to run with ``zope.schema`` >= 3.6.

- Made package fit to run on ZTK 1.1.

- Moved test dependencies to `test` extra.

- Using Python's ``doctest`` module instead of deprecated
  ``zope.testing.doctest``.


0.1
---

- Exception views are now unicode aware. They used to break on translated
  content.

- Added use_default_for_not_selected to Union field to use default
  value even if sub field is not selected.


===================
 CombinationWidget
===================

The combinationwidget collects two or more subfields to provide a convenient
way to specify a sequence of values.

Rendering the widget returns a table with the subfields::

    >>> from zc.form.browser.combinationwidget import (
    ...     CombinationWidget, CombinationDisplayWidget, default_template)
    >>> from zope import component, interface
    >>> component.provideAdapter(default_template, name='default')
    >>> from zc.form.field import Combination, OrderedCombinationConstraint
    >>> from zope.schema import Int
    >>> from zope.schema.interfaces import IInt
    >>> from zope.publisher.interfaces.browser import IBrowserRequest
    >>> from zope.formlib.interfaces import IInputWidget
    >>> from zope.formlib.textwidgets import IntWidget
    >>> component.provideAdapter(
    ...     IntWidget, (IInt, IBrowserRequest), IInputWidget)
    >>> from zope import interface
    >>> class IDemo(interface.Interface):
    ...     acceptable_count = Combination(
    ...         (Int(title=u'Minimum', required=True, min=0),
    ...          Int(title=u'Maximum', required=False)),
    ...         title=u'Acceptable Count',
    ...         required=False,
    ...         constraints=(OrderedCombinationConstraint(),))
    ...
    >>> from zope.publisher.browser import TestRequest
    >>> request = TestRequest()
    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)
    >>> widget.setPrefix('field')
    >>> widget.loadValueFromRequest() # None
    >>> print(widget())
    <input type='hidden' name='field.acceptable_count-marker' value='x' />
    <table class="combinationFieldWidget">
      <tr>
        <td class="label">
          <label for="field.acceptable_count.combination_00">
            <span class="required">*</span><span>Minimum</span>
          </label>
        </td>
        <td class="field">
          <div class="widget"><input class="textType"
            id="field.acceptable_count.combination_00"
            name="field.acceptable_count.combination_00" size="10" type="text"
            value=""  />
          </div>
        </td>
      </tr>
      <tr>
        <td class="label">
          <label for="field.acceptable_count.combination_01">
            <span>Maximum</span>
          </label>
        </td>
        <td class="field">
          <div class="widget"><input class="textType"
            id="field.acceptable_count.combination_01"
            name="field.acceptable_count.combination_01" size="10" type="text"
            value=""  />
          </div>
        </td>
      </tr>
    </table>

Setting the appropriate values in the Request lets the widget correctly read
the specified value::

    >>> request.form['field.acceptable_count-marker'] = 'x'
    >>> request.form['field.acceptable_count.combination_00'] = '10'
    >>> request.form['field.acceptable_count.combination_01'] = ''
    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)
    >>> widget.setPrefix('field')
    >>> widget.getInputValue()
    (10, None)
    >>> print(widget())
    <...
    ...<input class="textType" id="field.acceptable_count.combination_00"
              name="field.acceptable_count.combination_00" size="10" type="text"
              value="10" />...
    ...<input class="textType" id="field.acceptable_count.combination_01"
              name="field.acceptable_count.combination_01" size="10" type="text"
              value="" />...


The field is fine with empty values, because it is not required::

    >>> request.form['field.acceptable_count-marker'] = 'x'
    >>> request.form['field.acceptable_count.combination_00'] = ''
    >>> request.form['field.acceptable_count.combination_01'] = ''
    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)
    >>> widget.setPrefix('field')
    >>> widget.getInputValue() # None
    >>> print(widget())
    <...
    ...<input class="textType" id="field.acceptable_count.combination_00"
              name="field.acceptable_count.combination_00" size="10" type="text"
              value="" />...
    ...<input class="textType" id="field.acceptable_count.combination_01"
              name="field.acceptable_count.combination_01" size="10" type="text"
              value="" />...
    >>> bool(widget.error())
    False
    >>> bool(widget.widgets[0].error())
    False

If the optional value is filled in and the required one is not, though, there
are errors::

    >>> request.form['field.acceptable_count-marker'] = 'x'
    >>> request.form['field.acceptable_count.combination_00'] = ''
    >>> request.form['field.acceptable_count.combination_01'] = '10'
    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)
    >>> widget.setPrefix('field')
    >>> widget.getInputValue()
    Traceback (most recent call last):
    WidgetInputError: ('acceptable_count', u'Acceptable Count',
    WidgetInputError('combination_00', u'Minimum',
    RequiredMissing('combination_00')))
    >>> import zope.formlib.interfaces
    >>> import zope.publisher.interfaces.browser
    >>> @interface.implementer(zope.formlib.interfaces.IWidgetInputErrorView)
    ... @component.adapter(zope.formlib.interfaces.WidgetInputError,
    ...     zope.publisher.interfaces.browser.IBrowserRequest)
    ... class SnippetView(object):
    ...
    ...     def __init__(self, context, request):
    ...         self.context = context
    ...         self.request = request
    ...     def snippet(self):
    ...         return self.context.doc()
    ...
    >>> component.provideAdapter(SnippetView)
    >>> print(widget())
    <...
    ...<input class="textType" id="field.acceptable_count.combination_00"
              name="field.acceptable_count.combination_00" size="10"
              type="text" value="" />...
    ...Required input is missing...
    ...<input class="textType" id="field.acceptable_count.combination_01"
              name="field.acceptable_count.combination_01" size="10"
              type="text" value="10" />...
    >>> print(widget.error())
    Required input is missing.
    >>> print(widget.widgets[0].error())
    Required input is missing.

Similarly, if the field's constraints are not met, the widget shows errors::

    >>> request.form['field.acceptable_count-marker'] = 'x'
    >>> request.form['field.acceptable_count.combination_00'] = '20'
    >>> request.form['field.acceptable_count.combination_01'] = '10'
    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)
    >>> widget.setPrefix('field')
    >>> widget.getInputValue()
    Traceback (most recent call last):
    WidgetInputError: ('acceptable_count', u'Acceptable Count',
    MessageValidationError(u'${minimum} ...
    >>> print(widget())
    <...
    ...input class="textType" id="field.acceptable_count.combination_00"
              name="field.acceptable_count.combination_00" size="10"
              type="text" value="20" />...
    ...<input class="textType" id="field.acceptable_count.combination_01"
              name="field.acceptable_count.combination_01" size="10"
              type="text" value="10" />...
    >>> print(widget.error())
    ${minimum} must be less than or equal to ${maximum}.


There's also a display version of the widget::

    >>> request = TestRequest()
    >>> from zope.formlib.widget import DisplayWidget
    >>> from zope.formlib.interfaces import IDisplayWidget
    >>> component.provideAdapter(
    ...     DisplayWidget, (IInt, IBrowserRequest), IDisplayWidget)
    >>> widget = CombinationDisplayWidget(IDemo['acceptable_count'], request)
    >>> widget.setPrefix('field')
    >>> widget.setRenderedValue(('10', '2'))
    >>> print(widget())
    <input type='hidden' name='field.acceptable_count-marker' value='x' />
        <table class="combinationFieldWidget">
          <tr>
                  <td class="label">
                    <label for="field.acceptable_count.combination_00">
                      <span>Minimum</span>
                    </label>
                  </td>
              <td class="field">
                <div class="widget">10
                </div>
              </td>
          </tr>
          <tr>
                  <td class="label">
                    <label for="field.acceptable_count.combination_01">
                      <span>Maximum</span>
                    </label>
                  </td>
              <td class="field">
                <div class="widget">2
                </div>
              </td>
          </tr>
        </table>

In case of a wrong amount of parameters, the missing_value is used::

    >>> field = IDemo['acceptable_count']
    >>> field.missing_value=('23', '42')
    >>> widget = CombinationDisplayWidget(field, request)
    >>> widget.setPrefix('field')
    >>> widget.setRenderedValue(('10', '2', '3'))
    >>> print(widget())
    <input type='hidden' name='field.acceptable_count-marker' value='x' />
        <table class="combinationFieldWidget">
          <tr>
                  <td class="label">
                    <label for="field.acceptable_count.combination_00">
                      <span>Minimum</span>
                    </label>
                  </td>
              <td class="field">
                <div class="widget">23
                </div>
              </td>
          </tr>
          <tr>
                  <td class="label">
                    <label for="field.acceptable_count.combination_01">
                      <span>Maximum</span>
                    </label>
                  </td>
              <td class="field">
                <div class="widget">42
                </div>
              </td>
          </tr>
        </table>

In case the parameter is not a sequence, the missing_value is used::

    >>> widget = CombinationDisplayWidget(field, request)
    >>> widget.setPrefix('field')
    >>> widget.setRenderedValue(10)
    >>> print(widget())
    <input type='hidden' name='field.acceptable_count-marker' value='x' />
        <table class="combinationFieldWidget">
          <tr>
                  <td class="label">
                    <label for="field.acceptable_count.combination_00">
                      <span>Minimum</span>
                    </label>
                  </td>
              <td class="field">
                <div class="widget">23
                </div>
              </td>
          </tr>
          <tr>
                  <td class="label">
                    <label for="field.acceptable_count.combination_01">
                      <span>Maximum</span>
                    </label>
                  </td>
              <td class="field">
                <div class="widget">42
                </div>
              </td>
          </tr>
        </table>

The order of label and field are inverted in case of boolean::

    >>> request = TestRequest()
    >>> from zope.schema import Bool
    >>> from zope.schema.interfaces import IBool
    >>> from zope.formlib.boolwidgets import CheckBoxWidget
    >>> from zope.formlib.widget import DisplayWidget
    >>> from zope.formlib.interfaces import IDisplayWidget
    >>> component.provideAdapter(
    ...     CheckBoxWidget, (IBool, IBrowserRequest), IInputWidget)
    >>> class IBoolDemo(interface.Interface):
    ...     choices = Combination(
    ...         (Bool(title=u'first'),
    ...          Bool(title=u'second')),
    ...         title=u'Choices',
    ...         required=False,)

    >>> widget = CombinationWidget(IBoolDemo['choices'], request)
    >>> widget.setPrefix('field')
    >>> print(widget())
    <input type='hidden' name='field.choices-marker' value='x' />
        <table class="combinationFieldWidget">
          <tr>
                <td></td>
              <td class="field">
                <div class="widget"><input class="hiddenType" id="field.choices.combination_00.used" name="field.choices.combination_00.used" type="hidden" value="" /> <input class="checkboxType" id="field.choices.combination_00" name="field.choices.combination_00" type="checkbox" value="on"  />
                  <span>first</span>
                </div>
              </td>
          </tr>
          <tr>
                <td></td>
              <td class="field">
                <div class="widget"><input class="hiddenType" id="field.choices.combination_01.used" name="field.choices.combination_01.used" type="hidden" value="" /> <input class="checkboxType" id="field.choices.combination_01" name="field.choices.combination_01" type="checkbox" value="on"  />
                  <span>second</span>
                </div>
              </td>
          </tr>
        </table>



========================================
 Most Recently Used (MRU) Source Widget
========================================

The MRU widget keeps track of the last few values selected (on a per-principal
basis) and allows quickly selecting from that list instead of using a query
interface.

We can see the widget in action by using a custom form.  Let's define a schema
for the form that uses a source::

    >>> import zope.interface
    >>> import zope.schema

    >>> class IDemo(zope.interface.Interface):
    ...
    ...     color = zope.schema.Choice(
    ...         title=u"Color",
    ...         description=u"My favorite color",
    ...         source=AvailableColors,
    ...         )

And then a class that implements the interface::

    >>> @zope.interface.implementer(IDemo)
    ... class Demo(object):
    ...
    ...     color = None

We'll need a form that uses this schema::

    >>> import zope.formlib.form

    >>> class DemoInput(zope.formlib.form.EditForm):
    ...     actions = ()
    ...     form_fields = zope.formlib.form.fields(IDemo)

By rendering the form we can see that there are no MRU items to choose from
(because this principal has never visited this form before) and the query
interface is displayed::

    >>> import zope.publisher.browser
    >>> import zope.security.interfaces
    >>> import zope.security.management
    >>> import zope.component.hooks

    >>> @zope.interface.implementer(zope.security.interfaces.IPrincipal)
    ... class DummyPrincipal(object):
    ...
    ...     id = "someuser"
    ...     title = "Some User's Name"
    ...     description = "A User"

Note that we need to use the special resourcelibrary request.  We're
hacking together the TestRequest and the resourcelibrary request here; when we
switch to TestBrowser we can remove this oddity.

    >>> import zc.resourcelibrary.publication
    >>> class TestRequest(zope.publisher.browser.TestRequest,
    ...                   zc.resourcelibrary.publication.Request):
    ...     def _createResponse(self):
    ...         return zc.resourcelibrary.publication.Request._createResponse(
    ...             self)
    ...

    >>> request = TestRequest()
    >>> principal = DummyPrincipal()
    >>> request.setPrincipal(principal)
    >>> zope.security.management.newInteraction(request)

    >>> oldsite = zope.component.hooks.getSite()
    >>> zope.component.hooks.setSite(getRootFolder())

Now we can use an instance of our demo object to see that the form
pulls the possible values from the vocabulary we've defined above::

    >>> form = DemoInput(Demo(), request)
    >>> print(form())
    <...
    <div class="queries"...>
    <div class="query"...>
      <div class="queryinput"...>
        <query view for colors>
      </div> <!-- queryinput -->
    </div> <!-- query -->
    </div> <!-- queries -->
    ...

Note that the select box of MRU values isn't in the output, because the user
has never selected a value before::

    >>> '<select name="form.color">' not in form()
    True

Now, we can select one of the values::

    >>> zope.security.management.endInteraction()

    >>> request = TestRequest()
    >>> request.form = {
    ...     'form.color.query.selection': 'red_token',
    ...     'form.color.query.apply': 'Apply',
    ...     'form.color.displayed': '',
    ...     }
    >>> request.setPrincipal(principal)

    >>> zope.security.management.newInteraction(request)

Process the request and the list of MRU values is in the form::

    >>> form = DemoInput(Demo(), request)
    >>> print(form())
    <...
    <select name="form.color" id="form.color">
      <option value="red_token" selected="selected">Red</option>
    </select>
    ...

And the query view is hidden because we have an MRU list::

    >>> print(form())
    <...
    <input type="hidden" name="form.color.queries.visible" ... value="no">
    ...

If we select another value...::

    >>> request = TestRequest()
    >>> request.form = {
    ...     'form.color.query.selection': 'green_token',
    ...     'form.color.query.apply': 'Apply',
    ...     'form.color.displayed': '',
    ...     }
    >>> request.setPrincipal(principal)

...and process the request, the list of MRU values includes the new one, at
the top, and it is selected::

    >>> form = DemoInput(Demo(), request)
    >>> print(form())
    <...
    <select name="form.color" id="form.color">
      <option value="green_token" selected="selected">Green</option>
      <option value="red_token">Red</option>
    </select>
    ...

If we request a value not in the source everything stays the same, but nothing
is selected::

    >>> request = TestRequest()
    >>> request.form = {
    ...     'form.color.query.selection': 'blue_token',
    ...     'form.color.query.apply': 'Apply',
    ...     'form.color.displayed': '',
    ...     }
    >>> request.setPrincipal(principal)
    >>> form = DemoInput(Demo(), request)
    >>> print(form())
    <...
    <select name="form.color" id="form.color">
      <option value="green_token">Green</option>
      <option value="red_token">Red</option>
    </select>
    ...

We can make the query visible::

    >>> request = TestRequest()
    >>> request.form = {
    ...     'form.color.query.selection': 'red_token',
    ...     'form.color.query.apply': 'Apply',
    ...     'form.color.queries.visible': 'yes',
    ...     'form.color.query.search': 'yes',
    ...     'form.color.query.searchstring': 'red',
    ...     'form.color.displayed': '',
    ...     }
    >>> request.setPrincipal(principal)
    >>> form = DemoInput(Demo(), request)
    >>> print(form())
    <...
    <select name="form.color" id="form.color">
      <option value="red_token" selected="selected">Red</option>
      <option value="green_token">Green</option>
    </select>
    ...
    <select name="form.color.query.selection">
    <option value="red_token">Red</option>
    </select>
    <input type="submit" name="form.color.query.apply" value="Apply" />
    ...

It is not shown if the query is not applied::

    >>> request = TestRequest()
    >>> request.form = {
    ...     'form.color.query.selection': 'red_token',
    ...     'form.color.queries.visible': 'yes',
    ...     'form.color.query.search': 'yes',
    ...     'form.color.query.searchstring': 'red',
    ...     'form.color.displayed': '',
    ...     }
    >>> request.setPrincipal(principal)
    >>> form = DemoInput(Demo(), request)
    >>> print(form())
    <...
    <select name="form.color" id="form.color">
      <option value="red_token">Red</option>
      <option value="green_token">Green</option>
    </select>
    ...
    <select name="form.color.query.selection">
    <option value="red_token">Red</option>
    </select>
    <input type="submit" name="form.color.query.apply" value="Apply" />
    ...

Tokens in the annotation of the principal are ignored if they are not in the
source::

    >>> from zope.annotation.interfaces import IAnnotations
    >>> annotations = IAnnotations(principal)
    >>> annotation = annotations.get('zc.form.browser.mruwidget')
    >>> tokens = annotation.get('form.color')
    >>> tokens.append('black_token')
    >>> tokens
    ['red_token', 'green_token', 'black_token']

    >>> print(form())
    <...
    <select name="form.color" id="form.color">
      <option value="red_token">Red</option>
      <option value="green_token">Green</option>
    </select>
    ...
    <select name="form.color.query.selection">
    <option value="red_token">Red</option>
    </select>
    <input type="submit" name="form.color.query.apply" value="Apply" />
    ...


Clean up a bit::

    >>> zope.security.management.endInteraction()
    >>> zope.component.hooks.setSite(oldsite)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zopefoundation/zc.form",
    "name": "zc.form",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "zope formlib form widget extra",
    "author": "Zope Foundation and Contributors",
    "author_email": "zope-dev@zope.dev",
    "download_url": "https://files.pythonhosted.org/packages/bb/fb/53d7806eab867176be005bb81ac18736499f694504396d24fbff5d982883/zc.form-2.0.tar.gz",
    "platform": null,
    "description": "The zc.form package is a possibly temporary appendage used to hold extra\nbrowser widgets and alternative approaches to code found in the\nzope.formlib package.  Most or all of the code is created by Zope\nCorporation and is intended for eventual folding into the main Zope 3\nrelease.\n\n\n.. contents::\n\n=======\nChanges\n=======\n\n2.0 (2023-02-06)\n----------------\n\n- Add support for Python 3.8, 3.9, 3.10, 3.11.\n\n- Drop support for Python 2.7, 3.5, 3.6.\n\n\n1.1 (2019-02-11)\n----------------\n\n- Fix ZCML configuration issue if the ``[mruwidget]`` extra was not installed.\n\n\n1.0 (2019-01-11)\n----------------\n\nFeatures\n++++++++\n\n- Claim support for Python 3.5, 3.6, 3.7, PyPy and PyPy3.\n\nBugfixes\n++++++++\n\n- Fix a ``NameError`` in ``BaseVocabularyDisplay.render()``.\n\n- Actually pass a ``missing_value`` set on the ``Combination`` field to the\n  containing fields.\n\nCaveats\n+++++++\n\n- Installation of ``MruSourceInputWidget`` and ``TimeZoneWidget`` requires the\n  ``[mruwidget]`` extra to break dependency on ``zc.resourcelibrary`` for\n  projects which do not need it.\n\n\n0.5 (2016-08-02)\n----------------\n\n- Bind fields that are contained in a ``zc.form.field.Combination`` to fix the\n  ``context`` of those fields.\n\n\n0.4 (2016-01-12)\n----------------\n\n- Get rid of the `zope.app.pagetemplate` dependency.\n\n\n0.3 (2014-04-23)\n----------------\n\n- Remove requirement, that ``zc.form.field.Combination`` needs at least\n  two subfields.\n\n\n0.2 (2011-09-24)\n----------------\n\n- Got rid of ``zope.app.form`` dependency by requiring at least\n  ``zope.formlib`` 4.0.\n\n- Got rid of ``zope.app.component`` dependency by requiring at least\n  ``zope.component`` 3.8.\n\n- Depending on ``zope.catalog`` instead of ``zope.app.catalog``.\n\n- Depending on ``zope.security`` instead of ``zope.app.security``.\n\n- Depending on ``zope.app.wsgi`` >=3.7 instead of ``zope.app.testing`` for\n  test setup.\n\n- Depending on ``zope.browserpage`` and ``zope.container`` instead of\n  ``zope.app.publisher``.\n\n- Got rid of the following dependencies:\n\n  - ``zope.app.basicskin``\n  - ``zope.app.securitypolicy``\n  - ``zope.app.zapi``\n  - ``zope.app.zcmlfiles``\n\n- Fixed tests to run with ``zope.schema`` >= 3.6.\n\n- Made package fit to run on ZTK 1.1.\n\n- Moved test dependencies to `test` extra.\n\n- Using Python's ``doctest`` module instead of deprecated\n  ``zope.testing.doctest``.\n\n\n0.1\n---\n\n- Exception views are now unicode aware. They used to break on translated\n  content.\n\n- Added use_default_for_not_selected to Union field to use default\n  value even if sub field is not selected.\n\n\n===================\n CombinationWidget\n===================\n\nThe combinationwidget collects two or more subfields to provide a convenient\nway to specify a sequence of values.\n\nRendering the widget returns a table with the subfields::\n\n    >>> from zc.form.browser.combinationwidget import (\n    ...     CombinationWidget, CombinationDisplayWidget, default_template)\n    >>> from zope import component, interface\n    >>> component.provideAdapter(default_template, name='default')\n    >>> from zc.form.field import Combination, OrderedCombinationConstraint\n    >>> from zope.schema import Int\n    >>> from zope.schema.interfaces import IInt\n    >>> from zope.publisher.interfaces.browser import IBrowserRequest\n    >>> from zope.formlib.interfaces import IInputWidget\n    >>> from zope.formlib.textwidgets import IntWidget\n    >>> component.provideAdapter(\n    ...     IntWidget, (IInt, IBrowserRequest), IInputWidget)\n    >>> from zope import interface\n    >>> class IDemo(interface.Interface):\n    ...     acceptable_count = Combination(\n    ...         (Int(title=u'Minimum', required=True, min=0),\n    ...          Int(title=u'Maximum', required=False)),\n    ...         title=u'Acceptable Count',\n    ...         required=False,\n    ...         constraints=(OrderedCombinationConstraint(),))\n    ...\n    >>> from zope.publisher.browser import TestRequest\n    >>> request = TestRequest()\n    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)\n    >>> widget.setPrefix('field')\n    >>> widget.loadValueFromRequest() # None\n    >>> print(widget())\n    <input type='hidden' name='field.acceptable_count-marker' value='x' />\n    <table class=\"combinationFieldWidget\">\n      <tr>\n        <td class=\"label\">\n          <label for=\"field.acceptable_count.combination_00\">\n            <span class=\"required\">*</span><span>Minimum</span>\n          </label>\n        </td>\n        <td class=\"field\">\n          <div class=\"widget\"><input class=\"textType\"\n            id=\"field.acceptable_count.combination_00\"\n            name=\"field.acceptable_count.combination_00\" size=\"10\" type=\"text\"\n            value=\"\"  />\n          </div>\n        </td>\n      </tr>\n      <tr>\n        <td class=\"label\">\n          <label for=\"field.acceptable_count.combination_01\">\n            <span>Maximum</span>\n          </label>\n        </td>\n        <td class=\"field\">\n          <div class=\"widget\"><input class=\"textType\"\n            id=\"field.acceptable_count.combination_01\"\n            name=\"field.acceptable_count.combination_01\" size=\"10\" type=\"text\"\n            value=\"\"  />\n          </div>\n        </td>\n      </tr>\n    </table>\n\nSetting the appropriate values in the Request lets the widget correctly read\nthe specified value::\n\n    >>> request.form['field.acceptable_count-marker'] = 'x'\n    >>> request.form['field.acceptable_count.combination_00'] = '10'\n    >>> request.form['field.acceptable_count.combination_01'] = ''\n    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)\n    >>> widget.setPrefix('field')\n    >>> widget.getInputValue()\n    (10, None)\n    >>> print(widget())\n    <...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_00\"\n              name=\"field.acceptable_count.combination_00\" size=\"10\" type=\"text\"\n              value=\"10\" />...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_01\"\n              name=\"field.acceptable_count.combination_01\" size=\"10\" type=\"text\"\n              value=\"\" />...\n\n\nThe field is fine with empty values, because it is not required::\n\n    >>> request.form['field.acceptable_count-marker'] = 'x'\n    >>> request.form['field.acceptable_count.combination_00'] = ''\n    >>> request.form['field.acceptable_count.combination_01'] = ''\n    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)\n    >>> widget.setPrefix('field')\n    >>> widget.getInputValue() # None\n    >>> print(widget())\n    <...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_00\"\n              name=\"field.acceptable_count.combination_00\" size=\"10\" type=\"text\"\n              value=\"\" />...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_01\"\n              name=\"field.acceptable_count.combination_01\" size=\"10\" type=\"text\"\n              value=\"\" />...\n    >>> bool(widget.error())\n    False\n    >>> bool(widget.widgets[0].error())\n    False\n\nIf the optional value is filled in and the required one is not, though, there\nare errors::\n\n    >>> request.form['field.acceptable_count-marker'] = 'x'\n    >>> request.form['field.acceptable_count.combination_00'] = ''\n    >>> request.form['field.acceptable_count.combination_01'] = '10'\n    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)\n    >>> widget.setPrefix('field')\n    >>> widget.getInputValue()\n    Traceback (most recent call last):\n    WidgetInputError: ('acceptable_count', u'Acceptable Count',\n    WidgetInputError('combination_00', u'Minimum',\n    RequiredMissing('combination_00')))\n    >>> import zope.formlib.interfaces\n    >>> import zope.publisher.interfaces.browser\n    >>> @interface.implementer(zope.formlib.interfaces.IWidgetInputErrorView)\n    ... @component.adapter(zope.formlib.interfaces.WidgetInputError,\n    ...     zope.publisher.interfaces.browser.IBrowserRequest)\n    ... class SnippetView(object):\n    ...\n    ...     def __init__(self, context, request):\n    ...         self.context = context\n    ...         self.request = request\n    ...     def snippet(self):\n    ...         return self.context.doc()\n    ...\n    >>> component.provideAdapter(SnippetView)\n    >>> print(widget())\n    <...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_00\"\n              name=\"field.acceptable_count.combination_00\" size=\"10\"\n              type=\"text\" value=\"\" />...\n    ...Required input is missing...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_01\"\n              name=\"field.acceptable_count.combination_01\" size=\"10\"\n              type=\"text\" value=\"10\" />...\n    >>> print(widget.error())\n    Required input is missing.\n    >>> print(widget.widgets[0].error())\n    Required input is missing.\n\nSimilarly, if the field's constraints are not met, the widget shows errors::\n\n    >>> request.form['field.acceptable_count-marker'] = 'x'\n    >>> request.form['field.acceptable_count.combination_00'] = '20'\n    >>> request.form['field.acceptable_count.combination_01'] = '10'\n    >>> widget = CombinationWidget(IDemo['acceptable_count'], request)\n    >>> widget.setPrefix('field')\n    >>> widget.getInputValue()\n    Traceback (most recent call last):\n    WidgetInputError: ('acceptable_count', u'Acceptable Count',\n    MessageValidationError(u'${minimum} ...\n    >>> print(widget())\n    <...\n    ...input class=\"textType\" id=\"field.acceptable_count.combination_00\"\n              name=\"field.acceptable_count.combination_00\" size=\"10\"\n              type=\"text\" value=\"20\" />...\n    ...<input class=\"textType\" id=\"field.acceptable_count.combination_01\"\n              name=\"field.acceptable_count.combination_01\" size=\"10\"\n              type=\"text\" value=\"10\" />...\n    >>> print(widget.error())\n    ${minimum} must be less than or equal to ${maximum}.\n\n\nThere's also a display version of the widget::\n\n    >>> request = TestRequest()\n    >>> from zope.formlib.widget import DisplayWidget\n    >>> from zope.formlib.interfaces import IDisplayWidget\n    >>> component.provideAdapter(\n    ...     DisplayWidget, (IInt, IBrowserRequest), IDisplayWidget)\n    >>> widget = CombinationDisplayWidget(IDemo['acceptable_count'], request)\n    >>> widget.setPrefix('field')\n    >>> widget.setRenderedValue(('10', '2'))\n    >>> print(widget())\n    <input type='hidden' name='field.acceptable_count-marker' value='x' />\n        <table class=\"combinationFieldWidget\">\n          <tr>\n                  <td class=\"label\">\n                    <label for=\"field.acceptable_count.combination_00\">\n                      <span>Minimum</span>\n                    </label>\n                  </td>\n              <td class=\"field\">\n                <div class=\"widget\">10\n                </div>\n              </td>\n          </tr>\n          <tr>\n                  <td class=\"label\">\n                    <label for=\"field.acceptable_count.combination_01\">\n                      <span>Maximum</span>\n                    </label>\n                  </td>\n              <td class=\"field\">\n                <div class=\"widget\">2\n                </div>\n              </td>\n          </tr>\n        </table>\n\nIn case of a wrong amount of parameters, the missing_value is used::\n\n    >>> field = IDemo['acceptable_count']\n    >>> field.missing_value=('23', '42')\n    >>> widget = CombinationDisplayWidget(field, request)\n    >>> widget.setPrefix('field')\n    >>> widget.setRenderedValue(('10', '2', '3'))\n    >>> print(widget())\n    <input type='hidden' name='field.acceptable_count-marker' value='x' />\n        <table class=\"combinationFieldWidget\">\n          <tr>\n                  <td class=\"label\">\n                    <label for=\"field.acceptable_count.combination_00\">\n                      <span>Minimum</span>\n                    </label>\n                  </td>\n              <td class=\"field\">\n                <div class=\"widget\">23\n                </div>\n              </td>\n          </tr>\n          <tr>\n                  <td class=\"label\">\n                    <label for=\"field.acceptable_count.combination_01\">\n                      <span>Maximum</span>\n                    </label>\n                  </td>\n              <td class=\"field\">\n                <div class=\"widget\">42\n                </div>\n              </td>\n          </tr>\n        </table>\n\nIn case the parameter is not a sequence, the missing_value is used::\n\n    >>> widget = CombinationDisplayWidget(field, request)\n    >>> widget.setPrefix('field')\n    >>> widget.setRenderedValue(10)\n    >>> print(widget())\n    <input type='hidden' name='field.acceptable_count-marker' value='x' />\n        <table class=\"combinationFieldWidget\">\n          <tr>\n                  <td class=\"label\">\n                    <label for=\"field.acceptable_count.combination_00\">\n                      <span>Minimum</span>\n                    </label>\n                  </td>\n              <td class=\"field\">\n                <div class=\"widget\">23\n                </div>\n              </td>\n          </tr>\n          <tr>\n                  <td class=\"label\">\n                    <label for=\"field.acceptable_count.combination_01\">\n                      <span>Maximum</span>\n                    </label>\n                  </td>\n              <td class=\"field\">\n                <div class=\"widget\">42\n                </div>\n              </td>\n          </tr>\n        </table>\n\nThe order of label and field are inverted in case of boolean::\n\n    >>> request = TestRequest()\n    >>> from zope.schema import Bool\n    >>> from zope.schema.interfaces import IBool\n    >>> from zope.formlib.boolwidgets import CheckBoxWidget\n    >>> from zope.formlib.widget import DisplayWidget\n    >>> from zope.formlib.interfaces import IDisplayWidget\n    >>> component.provideAdapter(\n    ...     CheckBoxWidget, (IBool, IBrowserRequest), IInputWidget)\n    >>> class IBoolDemo(interface.Interface):\n    ...     choices = Combination(\n    ...         (Bool(title=u'first'),\n    ...          Bool(title=u'second')),\n    ...         title=u'Choices',\n    ...         required=False,)\n\n    >>> widget = CombinationWidget(IBoolDemo['choices'], request)\n    >>> widget.setPrefix('field')\n    >>> print(widget())\n    <input type='hidden' name='field.choices-marker' value='x' />\n        <table class=\"combinationFieldWidget\">\n          <tr>\n                <td></td>\n              <td class=\"field\">\n                <div class=\"widget\"><input class=\"hiddenType\" id=\"field.choices.combination_00.used\" name=\"field.choices.combination_00.used\" type=\"hidden\" value=\"\" /> <input class=\"checkboxType\" id=\"field.choices.combination_00\" name=\"field.choices.combination_00\" type=\"checkbox\" value=\"on\"  />\n                  <span>first</span>\n                </div>\n              </td>\n          </tr>\n          <tr>\n                <td></td>\n              <td class=\"field\">\n                <div class=\"widget\"><input class=\"hiddenType\" id=\"field.choices.combination_01.used\" name=\"field.choices.combination_01.used\" type=\"hidden\" value=\"\" /> <input class=\"checkboxType\" id=\"field.choices.combination_01\" name=\"field.choices.combination_01\" type=\"checkbox\" value=\"on\"  />\n                  <span>second</span>\n                </div>\n              </td>\n          </tr>\n        </table>\n\n\n\n========================================\n Most Recently Used (MRU) Source Widget\n========================================\n\nThe MRU widget keeps track of the last few values selected (on a per-principal\nbasis) and allows quickly selecting from that list instead of using a query\ninterface.\n\nWe can see the widget in action by using a custom form.  Let's define a schema\nfor the form that uses a source::\n\n    >>> import zope.interface\n    >>> import zope.schema\n\n    >>> class IDemo(zope.interface.Interface):\n    ...\n    ...     color = zope.schema.Choice(\n    ...         title=u\"Color\",\n    ...         description=u\"My favorite color\",\n    ...         source=AvailableColors,\n    ...         )\n\nAnd then a class that implements the interface::\n\n    >>> @zope.interface.implementer(IDemo)\n    ... class Demo(object):\n    ...\n    ...     color = None\n\nWe'll need a form that uses this schema::\n\n    >>> import zope.formlib.form\n\n    >>> class DemoInput(zope.formlib.form.EditForm):\n    ...     actions = ()\n    ...     form_fields = zope.formlib.form.fields(IDemo)\n\nBy rendering the form we can see that there are no MRU items to choose from\n(because this principal has never visited this form before) and the query\ninterface is displayed::\n\n    >>> import zope.publisher.browser\n    >>> import zope.security.interfaces\n    >>> import zope.security.management\n    >>> import zope.component.hooks\n\n    >>> @zope.interface.implementer(zope.security.interfaces.IPrincipal)\n    ... class DummyPrincipal(object):\n    ...\n    ...     id = \"someuser\"\n    ...     title = \"Some User's Name\"\n    ...     description = \"A User\"\n\nNote that we need to use the special resourcelibrary request.  We're\nhacking together the TestRequest and the resourcelibrary request here; when we\nswitch to TestBrowser we can remove this oddity.\n\n    >>> import zc.resourcelibrary.publication\n    >>> class TestRequest(zope.publisher.browser.TestRequest,\n    ...                   zc.resourcelibrary.publication.Request):\n    ...     def _createResponse(self):\n    ...         return zc.resourcelibrary.publication.Request._createResponse(\n    ...             self)\n    ...\n\n    >>> request = TestRequest()\n    >>> principal = DummyPrincipal()\n    >>> request.setPrincipal(principal)\n    >>> zope.security.management.newInteraction(request)\n\n    >>> oldsite = zope.component.hooks.getSite()\n    >>> zope.component.hooks.setSite(getRootFolder())\n\nNow we can use an instance of our demo object to see that the form\npulls the possible values from the vocabulary we've defined above::\n\n    >>> form = DemoInput(Demo(), request)\n    >>> print(form())\n    <...\n    <div class=\"queries\"...>\n    <div class=\"query\"...>\n      <div class=\"queryinput\"...>\n        <query view for colors>\n      </div> <!-- queryinput -->\n    </div> <!-- query -->\n    </div> <!-- queries -->\n    ...\n\nNote that the select box of MRU values isn't in the output, because the user\nhas never selected a value before::\n\n    >>> '<select name=\"form.color\">' not in form()\n    True\n\nNow, we can select one of the values::\n\n    >>> zope.security.management.endInteraction()\n\n    >>> request = TestRequest()\n    >>> request.form = {\n    ...     'form.color.query.selection': 'red_token',\n    ...     'form.color.query.apply': 'Apply',\n    ...     'form.color.displayed': '',\n    ...     }\n    >>> request.setPrincipal(principal)\n\n    >>> zope.security.management.newInteraction(request)\n\nProcess the request and the list of MRU values is in the form::\n\n    >>> form = DemoInput(Demo(), request)\n    >>> print(form())\n    <...\n    <select name=\"form.color\" id=\"form.color\">\n      <option value=\"red_token\" selected=\"selected\">Red</option>\n    </select>\n    ...\n\nAnd the query view is hidden because we have an MRU list::\n\n    >>> print(form())\n    <...\n    <input type=\"hidden\" name=\"form.color.queries.visible\" ... value=\"no\">\n    ...\n\nIf we select another value...::\n\n    >>> request = TestRequest()\n    >>> request.form = {\n    ...     'form.color.query.selection': 'green_token',\n    ...     'form.color.query.apply': 'Apply',\n    ...     'form.color.displayed': '',\n    ...     }\n    >>> request.setPrincipal(principal)\n\n...and process the request, the list of MRU values includes the new one, at\nthe top, and it is selected::\n\n    >>> form = DemoInput(Demo(), request)\n    >>> print(form())\n    <...\n    <select name=\"form.color\" id=\"form.color\">\n      <option value=\"green_token\" selected=\"selected\">Green</option>\n      <option value=\"red_token\">Red</option>\n    </select>\n    ...\n\nIf we request a value not in the source everything stays the same, but nothing\nis selected::\n\n    >>> request = TestRequest()\n    >>> request.form = {\n    ...     'form.color.query.selection': 'blue_token',\n    ...     'form.color.query.apply': 'Apply',\n    ...     'form.color.displayed': '',\n    ...     }\n    >>> request.setPrincipal(principal)\n    >>> form = DemoInput(Demo(), request)\n    >>> print(form())\n    <...\n    <select name=\"form.color\" id=\"form.color\">\n      <option value=\"green_token\">Green</option>\n      <option value=\"red_token\">Red</option>\n    </select>\n    ...\n\nWe can make the query visible::\n\n    >>> request = TestRequest()\n    >>> request.form = {\n    ...     'form.color.query.selection': 'red_token',\n    ...     'form.color.query.apply': 'Apply',\n    ...     'form.color.queries.visible': 'yes',\n    ...     'form.color.query.search': 'yes',\n    ...     'form.color.query.searchstring': 'red',\n    ...     'form.color.displayed': '',\n    ...     }\n    >>> request.setPrincipal(principal)\n    >>> form = DemoInput(Demo(), request)\n    >>> print(form())\n    <...\n    <select name=\"form.color\" id=\"form.color\">\n      <option value=\"red_token\" selected=\"selected\">Red</option>\n      <option value=\"green_token\">Green</option>\n    </select>\n    ...\n    <select name=\"form.color.query.selection\">\n    <option value=\"red_token\">Red</option>\n    </select>\n    <input type=\"submit\" name=\"form.color.query.apply\" value=\"Apply\" />\n    ...\n\nIt is not shown if the query is not applied::\n\n    >>> request = TestRequest()\n    >>> request.form = {\n    ...     'form.color.query.selection': 'red_token',\n    ...     'form.color.queries.visible': 'yes',\n    ...     'form.color.query.search': 'yes',\n    ...     'form.color.query.searchstring': 'red',\n    ...     'form.color.displayed': '',\n    ...     }\n    >>> request.setPrincipal(principal)\n    >>> form = DemoInput(Demo(), request)\n    >>> print(form())\n    <...\n    <select name=\"form.color\" id=\"form.color\">\n      <option value=\"red_token\">Red</option>\n      <option value=\"green_token\">Green</option>\n    </select>\n    ...\n    <select name=\"form.color.query.selection\">\n    <option value=\"red_token\">Red</option>\n    </select>\n    <input type=\"submit\" name=\"form.color.query.apply\" value=\"Apply\" />\n    ...\n\nTokens in the annotation of the principal are ignored if they are not in the\nsource::\n\n    >>> from zope.annotation.interfaces import IAnnotations\n    >>> annotations = IAnnotations(principal)\n    >>> annotation = annotations.get('zc.form.browser.mruwidget')\n    >>> tokens = annotation.get('form.color')\n    >>> tokens.append('black_token')\n    >>> tokens\n    ['red_token', 'green_token', 'black_token']\n\n    >>> print(form())\n    <...\n    <select name=\"form.color\" id=\"form.color\">\n      <option value=\"red_token\">Red</option>\n      <option value=\"green_token\">Green</option>\n    </select>\n    ...\n    <select name=\"form.color.query.selection\">\n    <option value=\"red_token\">Red</option>\n    </select>\n    <input type=\"submit\" name=\"form.color.query.apply\" value=\"Apply\" />\n    ...\n\n\nClean up a bit::\n\n    >>> zope.security.management.endInteraction()\n    >>> zope.component.hooks.setSite(oldsite)\n",
    "bugtrack_url": null,
    "license": "ZPL 2.1",
    "summary": "Extra browser widgets and alternative approaches for zope.formlib.",
    "version": "2.0",
    "split_keywords": [
        "zope",
        "formlib",
        "form",
        "widget",
        "extra"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92a5f2a97049ba1cee064d4065a17417fc030c23c371252ebcf1991a3196e914",
                "md5": "137dc6e87d6ad406a044b5c6a7420807",
                "sha256": "f47e32a31ff76cfbb7edf29145d520772fe26d64af26966b5cef3e56d30af710"
            },
            "downloads": -1,
            "filename": "zc.form-2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "137dc6e87d6ad406a044b5c6a7420807",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 49653,
            "upload_time": "2023-02-06T14:06:27",
            "upload_time_iso_8601": "2023-02-06T14:06:27.255222Z",
            "url": "https://files.pythonhosted.org/packages/92/a5/f2a97049ba1cee064d4065a17417fc030c23c371252ebcf1991a3196e914/zc.form-2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bbfb53d7806eab867176be005bb81ac18736499f694504396d24fbff5d982883",
                "md5": "1ddd87e12db9a5b0cef936d0995b019f",
                "sha256": "832bfac159b96de2e262c15a6d35a89ca495bf835424f2602f55bd1befce4b71"
            },
            "downloads": -1,
            "filename": "zc.form-2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "1ddd87e12db9a5b0cef936d0995b019f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 41432,
            "upload_time": "2023-02-06T14:06:29",
            "upload_time_iso_8601": "2023-02-06T14:06:29.002662Z",
            "url": "https://files.pythonhosted.org/packages/bb/fb/53d7806eab867176be005bb81ac18736499f694504396d24fbff5d982883/zc.form-2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-06 14:06:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "zopefoundation",
    "github_project": "zc.form",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "zc.form"
}
        
Elapsed time: 0.03911s