Plone/z3c.form 3.2.1- How can I make an autocomplete widget (that is not a required field) use a custom binded source object?

血红的双手。 提交于 2019-12-12 17:26:07

问题


I'm trying to get an autocomplete widget to render in a form (z3c.form), using a binded source object.

In the interface class:

Parent = schema.Choice(title=u'A Parent',
                       source=ParentSourceBinder(),
                       required=False,
                       default=None)

In my form.Form class:

fields['Parent'].widgetFactory = AutocompleteFieldWidget    

I'm using a custom source binder because I need to pull data from a database.

class ParentSource(object):
    implements(IQuerySource)

    vocabulary = SimpleVocabulary([])
    session = None
    __iter__ = vocabulary.__iter__

    def __init__(self, context):
        self.context=context
        if self.session is None:
            db_utility = queryUtility(IMyDBUtility,name="MyDBUtility")
            self.session = db_utility.session
        self.vocabulary = SimpleVocabulary([SimpleTerm(title='temp',value='temp',token='temp'),])

Then I have the __contains__, getTerm, getTermByToken, search functions.

When required is set to false and I try going to load the form, I get an error: LookupError:--NOVALUE--, which pointed to getTermByToken found in SimpleVocabulary in zope.schema.vocabulary.

Tinkering around with the getTermByToken function in zope.schema.vocabulary's SimpleVocabulary, I was able to determine that the problem was related to z3c.form.browser.radio, which the AutocompleteFieldWidget does use a radio set.

I have two versions of z3c.form available, 3.0.5 and 3.2.1, which is the default if the version is not set in the buildout. The radio.py's of both versions are different. When I set the version in buildout.cfg to 3.0.5, the form renders and the autocomplete widget works with required set to False. 3.2.1 leaves me with the LookupError.

How can I get the autocomplete widget to work without needing to set required to true and use version 3.2.1 at the same time?

Edit: I'm importing the AutocompleteFieldWidget from plone.formwidget.autocomplete. The version of plone I am using is 4.3.4.1

Update I found something interesting with the radio_input.pt files of 3.0.5 and 3.2.1 The line where the widget is rendered is different.

3.0.1 has:

   input tal:replace="structure python:view.renderForValue(item['value'])"

3.2.1 has:

   input id="" name="" class="" alt="" title=""
   tabindex="" disabled="" readonly="" accesskey="" value=""
   checked="" type="radio"
   tal:define="checked item/checked"
   tal:attributes="id item/id;
                   name item/name;
                   class view/klass;
                   value item/value;
                   style view/style;
                   title view/title;
                   lang view/lang;
                   ...

When I replaced 3.2.1's input with 3.0.5's input, the widget was working correctly.

来源:https://stackoverflow.com/questions/29805675/plone-z3c-form-3-2-1-how-can-i-make-an-autocomplete-widget-that-is-not-a-requi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!