简体   繁体   English

FormEncode中的ForEach和NestedVariables在Pyramid中创建表单项的数组

[英]ForEach and NestedVariables in FormEncode to create array of form items in Pyramid

I'm using Pyramid with FormEncode to try and create and validate a list of addresses. 我正在使用Pyramid和FormEncode来尝试创建和验证地址列表。 I'm using pyramid_simpleform and have been looking at this tutorial http://jimmyg.org/blog/2007/multiple-checkboxes-with-formencode.html and this previous question Pylons FormEncode with an array of form elements but I'm still having some issues. 我正在使用pyramid_simpleform并且一直在查看本教程http://jimmyg.org/blog/2007/multiple-checkboxes-with-formencode.html以及之前的问题Pylons FormEncode,其中包含一系列表单元素,但我仍然有一些问题。 My structure is currently as follows: 我的结构目前如下:

Schema: 架构:

from formencode import Schema, validators, ForEach, NestedVariables

class AddressSchema(Schema):
allow_extra_fields = False
addresses = validators.String(not_empty=True)

class JobSchema(Schema):
filter_extra_fields = True
allow_extra_fields = True
pre_validators = [NestedVariables()]
job_name = validators.MinLength(5, not_empty=True)
comments = validators.MinLength(5, not_empty=False)
addresses = ForEach(AddressSchema())

Template: 模板:

${renderer.errorlist("addresses")}
${renderer.errorlist("job_name")}

<p><label for="job_name">Job name: </label>${renderer.text("job_name", size=30)}</p>
% for a in range(1, initial_number_of_address_fields):
    <p><label for="addresses-${a}">Address: </label>${renderer.textarea("addresses-" + str(a), cols=39, rows=6)}</p>
% endfor
${renderer.submit("submit", "Submit")}

View: 视图:

@view_config(route_name='add_addresses', renderer="add_addresses.mak")
def add_addresses(request):

from myproject.forms import JobSchema
from myproject.models import Job
from formencode import htmlfill, variabledecode, ForEach

initial_number_of_address_fields = 5

form = Form(request, schema=JobSchema(), variable_decode=False)
renderer = FormRenderer(form)

# if the form has been submitted
if 'submit' in request.POST:

    if form.validate(): # uses validation specified in forms.py

        # automatically bind to provided form
        obj = form.bind(Job()) # no exisiting id provided, so a new document is created
        # add some additional values
        obj.__setattr__("last_updated_on", datetime.date.today().strftime('%Y/%m/%d'))
        #save
        obj.save()
        return HTTPFound(location="/")

return {
    'title':'Add addresses',
    'initial_number_of_address_fields': initial_number_of_address_fields, 
    'renderer': renderer
}

I get back actual validation errors like so: 我得到了实际验证错误,如下所示:

{'addresses': u'Missing value'}

But properly filled out values also provide an error: 但正确填写的值也会出错:

The input must be dict-like (not a : u'dgfgfd') 输入必须是类似dict的(不是:u'dgfgfd')

If I change variable_decode to True (in the form variable setup) I no longer get any errors back at all. 如果我将variable_decode更改为True (在表单变量设置中),我根本不会再收到任何错误。 I think I'm supposed to be using variable_decode somehow, but I'm not sure how. 我想我应该以某种方式使用variable_decode ,但我不确定如何。 How do I properly validate these values? 如何正确验证这些值?

I have written a blog post with similar usage back in 2009, it may come in handy: 我在2009年撰写了一篇类似用法的博文,它可能会派上用场:

http://www.domenkozar.com/2009/07/22/advanced-formencode-usage/ http://www.domenkozar.com/2009/07/22/advanced-formencode-usage/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM