链接z3c表单

2024-05-20 21:01:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想能够连锁几个z3c形式的Plone一个接一个。例如,一旦form#1完成处理并完成错误检查,它将结果(最好是通过GET变量)传递到form#2,而form#2又对form#3等执行相同的操作。。。我也希望能够使用相同的网址为所有的表格。在

我当前的实现是拥有一个浏览器视图,然后发送适当的表单,即DispatcherView检查自我请求变量,然后确定调用窗体1、窗体2、窗体3中的哪一个。在

我有这段代码,但是看起来z3c表单被抽象为对BrowserView的几个调用,并且试图触发从它到z3c的多个调用会干扰后者的处理。例如,当用户按一次“提交”按钮时,会对表单1进行错误检查,当我尝试下面示例中的解决方案时,表单2返回显示所有必需字段都不正确,这意味着表单2从表单1接收到值。我尝试从不同的地方触发form#2,比如DispatcherView(BrowserView)调用()方法,调用()表单1的方法,以及后者的update()和render(),但所有这些重写都会导致相同的问题。在

什么地方是合适的地方来搭载连续的调用,这样就可以工作了,或者我需要创建单独的页面并使用自我请求响应.重新更正?在

from Products.Five import BrowserView
from zope import interface, schema
from z3c.form import form, field, group, button
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

countries = SimpleVocabulary([SimpleTerm(value="not_selected", title=_("Chose your region")),
                            SimpleTerm(value="canada", title=_("Canada")),
                            SimpleTerm(value="us", title=_("United States")),
                            SimpleTerm(value="belgium", title=_("Belgium"))])
products =  SimpleVocabulary([SimpleTerm(value="product1", title=_("Product1")),
                            SimpleTerm(value="product2", title=_("Product2")),
                            SimpleTerm(value="product3", title=_("Product2"))
                            ])
class DispatcherView(BrowserView):
    def __call__(self):
        if 'form.widgets.region' in self.request.keys():
            step2 = Step2(self.context, self.request)
            return step2.__call__()
        else:
            step1 = Step1(self.context, self.request)
            return step1.__call__() 
    def update(self):
        pass

class IStep1(interface.Interface):
    region = schema.Choice(title=_("Select your region"),
                        vocabulary=countries, required=True,
                        default="not_selected")
class IStep2(interface.Interface):
    product = schema.Choice(title=_("Pick a product"),
                        vocabulary=products, required=True)

class Step1(form.Form):
    fields = field.Fields(IStep1)
    def __init__(self,context, request):
        self.ignoreContext = True
        super(self.__class__, self).__init__(context, request)
    def update(self):
        super(self.__class__, self).update()
    @button.buttonAndHandler(u'Next >>')
    def handleNext(self, action):
        data, errors = self.extractData()
        if errors:
            print "Error occured"

class Step2(form.Form):
    fields = field.Fields(IStep2)
    def __init__(self,context, request):
        self.ignoreContext = True
        super(self.__class__, self).__init__(context, request)
    def update(self):
        super(self.__class__, self).update()
    @button.buttonAndHandler(_('<< Previous'))
    def handleBack(self, action):
        data, errors = self.extractData()
        if errors:
            print "Error occured"
            #handle input errors here

    @button.buttonAndHandler(_('Next >>'))
    def handleNext(self, action):
        data, errors = self.extractData()
        if errors:
            print "Error occured"

编辑: Cris Ewing对此给出了答案,下面是使用collective.z3cformwizard重写后的示例代码:

^{pr2}$

也别忘了浏览器:视图段塞在配置.zcml公司名称:

<browser:page
    name="view"
    for="Products.myproduct.DispatcherView"
    class=".file.DispatcherView"
    permission="zope2.View"
/>

Tags: z3cselfform表单titlevaluerequestdef