表单验证的正确方法是哪一种? Colander的模式验证或Deform的表单验证? [英] Which one is the correct approach for form validation ? Colander's Schema validation or Deform's form validation?

查看:98
本文介绍了表单验证的正确方法是哪一种? Colander的模式验证或Deform的表单验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始为我的一个项目使用金字塔我需要通过获取表单字段值并进行Web服务调用以断言该值的正确性来验证表单字段输入.例如,有一个名为您的银行的CUSTOMER-ID的字段.我需要以(单独)作为输入并通过发出Web服务调用(例如http://someotherdomain/validate_customer_id/?customer_id=<input_value>)在服务器级别进行验证.

I have just started using Pyramid for one of my projects and I have a case where in I need to validate a form field input, by taking that form field value and making a web-service call to assert the value's correctness. Like for example there is a field called your bank's CUSTOMER-ID. I need to take that(alone) as input and validate at the server level by making a web-service call (like http://someotherdomain/validate_customer_id/?customer_id=<input_value>)lets say.

我正在使用 Colander 进行表单架构管理和变形.我对需要在哪里放置CUSTOMER-ID案例的验证逻辑感到困惑.是在MySchema().bind(customer_id=<input_value>)(具有延迟的验证器来查询Web服务)还是在form.validate(request.POST.items())处?如果我采用了延迟验证程序的路径,则MySchema().bind对于不正确的CUSTOMER-ID会引发colander.Invalid错误.没关系.但是,该错误不是在表单级别,而是在模式级别.那么我该如何以理智的方式告诉用户呢?

I am using Colander for form schema management and Deform for all form validation logic. I am confused about where I need to place my validation logic for the CUSTOMER-ID case. Is it at MySchema().bind(customer_id=<input_value>) (which has a deferred validator that queries the web-service) or something at the form.validate(request.POST.items()) ? If I take the deferred validator's path, then MySchema().bind is raising colander.Invalid error for incorrect CUSTOMER-ID. Thats fine. But that error is not at the form level but at the schema level. So how would I tell the user about this in a sane way ?

我对Django表单有很好的经验,所以我期望使用类似clean方法的方法.我期望在模板级别出现像form ['customer_id'].error这样的表单错误.金字塔变形或漏勺是否可能?

I have good experience with Django forms so I was expecting something like clean method. A form error like form['customer_id'].error is what I am expecting at the template level. Is it possible with Pyramid's Deform or with Colander ?

推荐答案

所以我认为您遇到的最大问题是了解Colander和Deform的关注点分离.人们喜欢将Colander称为通用模式验证库.这意味着我们定义了一个架构,其中每个节点都有特定的数据类型,并且某些节点可能是必需的/可选的.然后,Colander能够验证该模式,并告诉我们传递给Colander的数据是否符合该模式.例如,在我的Web应用程序中,我经常构建接受需要验证的GET/POST参数的api.因此,在金字塔中,假设我有这种情况:

So I think the big problem you're having is understanding the separation of concerns of Colander and Deform. Colander is what people like to call a general schema validation library. Which means we define a schema, where each node has a particular data type and some nodes might be required/optional. Colander is then able to validate that schema, and tell us whether or no the data we passed to colander conforms to that schema. As an example, in my web apps, I am often building apis that accept GET/POST params that need to be validated. So in Pyramid, let's say I have this scenario:

request.POST = {
    'post_id': 1,
    'author_id': 1,
    'unnecessary_attr': 'stuff'
}

然后我可以像这样验证它:

I can then validate it like so:

# schema
schema = SchemaNode(Mapping(),
                    SchemaNode(Integer(), name='post_id'),
                    SchemaNode(Integer(), name='author_id'))
schema.deserialize(request.POST)

如果它不能使数据符合指定的模式,它将出错.因此,您可以看到,漏勺实际上可以用于验证任何数据集,而无论这些数据来自POST/GET/JSON数据.另一方面,变形是一个表单库,可帮助您创建/验证表单.它使用漏勺满足所有验证需求,并且很明显只是将验证完全委托给滤锅.因此,要回答您的问题,您将使用漏勺进行所有验证工作,而变形将主要处理表单的呈现.

And it will error if it can't conform the data to the specified schema. So you can see, colander can actually be used to validate ANY set of data, whether that comes from POST/GET/JSON data. Deform on the other hand is a form library, and helps you create/validate forms. It uses colander for all of the validation needs and as you can see it pretty much just completely delegates validation to colander. So to answer your question, you would do all of your validation stuff in colander, and deform would mostly handle the rendering of your forms.

这篇关于表单验证的正确方法是哪一种? Colander的模式验证或Deform的表单验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆