.. -*-doctest-*-

==========================================
Choice Fields with Vocabularies or Sources
==========================================

The schema editor allows the user to add Choice fields while also
specifying a vocabulary or source of the values which can be
selected.

Log in as a user who can edit content type schemata and open the
schema editor.

    >>> user = app.acl_users.userFolderAddUser(
    ...     'root', 'secret', ['Manager'], [])

    >>> from Products.Five import testbrowser
    >>> browser = testbrowser.Browser()
    >>> browser.handleErrors = False
    >>> browser.addHeader('Authorization', 'Basic root:secret')

Open the schema editor in the browser.

    >>> portal_url = 'http://nohost'
    >>> browser.open(portal_url + '/@@schemaeditor')
    >>> 'Edit @@schemaeditor' in browser.contents
    True

Add a Choice field.

    >>> browser.getControl('Add new field').click()
    >>> browser.getControl('Title').value = 'Country'
    >>> browser.getControl('Short Name').value = 'country'
    >>> browser.getControl('Field type').getControl(
    ...     value='Choice').selected = True
    >>> browser.getControl('Add').click()
    [event: ObjectAddedEvent on Choice]
    [event: SchemaModifiedEvent on DummySchemaContext]
    >>> browser.url
    'http://nohost/@@schemaeditor'

Open the new fields edit form.

    >>> browser.getLink(url='country').click()

The edit form for choice fields includes a widget for specifying the
vocabulary.

    >>> ctl = browser.getControl('Vocabulary')
    >>> ctl
    <Control name='form.widgets.values' type='textarea'>

If duplicate values are entered an error is raised.

    >>> ctl.value = '\n'.join(
    ...     ['Alaska', 'Russia', 'United States', 'United States',
    ...      'Other'])
    >>> browser.getControl('Save').click()
    >>> print browser.contents
    <...
      <div class="error">The 'United States' vocabulary value conflicts with 'United States'.</div>
    ...

For now, the default and missing_value fields are TextLine fields.

    >>> browser.getControl('Default')
    <Control name='form.widgets.default' type='text'>
    >>> browser.getControl('Missing Value')
    <Control name='form.widgets.missing_value' type='text'>

Enter unique values and save the field settings.

    >>> browser.getControl('Vocabulary').value = '\n'.join(
    ...     ['Alaska', 'Russia', 'United States', "C\xc3\xb4te d'Ivoire", 'Other'])
    >>> browser.getControl('Save').click()
    [event: ObjectModifiedEvent on Choice]
    [event: SchemaModifiedEvent on DummySchemaContext]

When the edit form for the content type is loaded, the vocabulary
values specified can be selected.

    >>> browser.open(portal_url + '/@@contexteditor')
    >>> ctl = browser.getControl('Country')
    >>> item = ctl.getControl('Russia')
    >>> item
    <ItemControl name='form.widgets.country:list' type='select'
    optionValue='Russia' selected=False>
    >>> item.selected = True
    >>> ctl.value
    ['Russia']
    >>> item = ctl.getControl('Alaska')
    >>> item.selected
    False
    >>> item.selected = True
    >>> ctl.getControl('Russia').selected
    False
    >>> ctl.value
    ['Alaska']

Multiple Choice
===============

A vocabulary of simple values can also be used with a multiple
selection field.

Open the schema editor in the browser.

    >>> browser.open(portal_url + '/@@schemaeditor')
    >>> 'Edit @@schemaeditor' in browser.contents
    True

Add a Choice field.

    >>> browser.getControl('Add new field').click()
    >>> browser.getControl('Title').value = 'Categories'
    >>> browser.getControl('Short Name').value = 'categories'
    >>> browser.getControl('Field type').getControl(
    ...     'Multiple Choice').selected = True
    >>> browser.getControl('Add').click()
    [event: ObjectAddedEvent on List]
    [event: SchemaModifiedEvent on DummySchemaContext]
    >>> browser.url
    'http://nohost/@@schemaeditor'

Open the new fields edit form.

    >>> browser.getLink(url='categories').click()

The edit form for choice fields includes a widget for specifying the
vocabulary.

    >>> ctl = browser.getControl('Vocabulary')
    >>> ctl
    <Control name='form.widgets.values' type='textarea'>

If duplicate values are entered an error is raised.

    >>> ctl.value = '\n'.join(
    ...     ['Lisp', 'Plone', 'Python', 'Lisp'])
    >>> browser.getControl('Save').click()
    >>> print browser.contents
    <...
      <div class="error">The 'Lisp' vocabulary value conflicts with 'Lisp'.</div>
    ...

For now, the default and missing_value fields are Text fields.

    >>> browser.getControl('Default')
    <Control name='form.widgets.default' type='textarea'>
    >>> browser.getControl('Missing Value')
    <Control name='form.widgets.missing_value' type='textarea'>

Enter unique values and save the field settings.

    >>> browser.getControl('Vocabulary').value = '\n'.join(
    ...     ['Plone', 'Python', 'Lisp'])
    >>> browser.getControl('Default').value = 'Plone'
    >>> browser.getControl('Save').click()
    [event: ObjectModifiedEvent on List]
    [event: SchemaModifiedEvent on DummySchemaContext]

When the edit form for the content type is loaded, the vocabulary
values specified can be selected.

    >>> browser.open(portal_url + '/@@contexteditor')
    >>> from_ctl = browser.getControl(name="form.widgets.categories.from")
    >>> from_ctl
    <ListControl name='form.widgets.categories.from' type='select'>
    >>> to_ctl = browser.getControl(name="form.widgets.categories.to")
    >>> item = to_ctl.getControl('Plone')
    >>> item
    <ItemControl name='form.widgets.categories.to' type='select'
    optionValue='Plone' selected=False>

    >>> item = from_ctl.getControl('Python')
    >>> to_ctl.mech_control.items.append(item.mech_item)

    >>> from_ctl.getControl('Lisp').selected
    False
    >>> to_ctl.options
    ['Plone', 'Python']
