Formencode 条件验证
如何根据另一个字段的存在有条件地验证一个字段。例如,仅当“国家”为“美国”时才需要“州”。
谢谢, 史蒂夫
编辑:
所以我想这样做:
chained_validators = [validators.RequireIfPresent('state', present="country")]
但错误消息与“_the_form”而不是“state”相关。有没有办法将其链接到该字段?
how do i validate a field conditionally based on the presence of another field. for example, only make "state" required only if "country" is "US".
thanks,
steve
EDIT:
so i figured to do this:
chained_validators = [validators.RequireIfPresent('state', present="country")]
but the error message is associated with "_the_form" instead of "state". is there a way to link it to the field instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我公司做项目的时候也遇到了同样的问题。我们为此编写了自己的 Formencode 验证器。我们目前正在尝试将其与主项目合并。同时您可以在这里下载:https://github.com/GevatterGaul/formencode
还有德语的 Howto:http://techblog.auf-nach-mallorca.info/2014/08/ 19/dynamische_formulare_validieren_mit_formencode/
但是让我快速给你介绍一下示例上下文中的概要:
主要好处是,与
validators.RequireIfPresent
相比,validators.RequireIfMatching
仅当给定字段与给定值匹配时才需要一个字段。在您的示例中,仅当“国家/地区”为“美国”时,才需要“州”字段。希望我能帮忙。
Had the same problem during a project in my company. We wrote our own Formencode validator for this. We currently try to merge it with the main project. In the meantime you can download it here: https://github.com/GevatterGaul/formencode
There is also a Howto in, well, german: http://techblog.auf-nach-mallorca.info/2014/08/19/dynamische_formulare_validieren_mit_formencode/
But let me give you a quick rundown in the context of your example:
The main benefit is, that in comparison to
validators.RequireIfPresent
,validators.RequireIfMatching
only requires a field when a given field matches a given value. In your example, only if 'country' is 'US', then it requires the 'state' field.Hope I could help.