V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

WTForms 验证表单时,依赖其他表单 field,怎么办?

  •  
  •   miniyao · 2018-05-23 10:09:43 +08:00 · 1320 次点击
    这是一个创建于 2162 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在验证 field-b 时,要依赖 field-a 的内容,怎么在 validate_field-b() 中,传入 field-a 的内容?

    class ItemForm(Form):
        field-a = StringField('Field A')
        field-b = StringField('Field B')
        submit = SubmitField('Submit')
        def validate_field-b(self, field):
            if Item.query.filter_by(item_id=field.data).first():
                raise ValidationError('Existed!')
    

    field-a 是 Item 的大类目编号,field-b 是 Item 的详细编码,但是数据库里存放的时候,前任把 field-a 和 field-b 拼接在一起存进去了,我想在表单的前端就验证是否有重复(须要匹配 item_id=field-a.data + field-b.data ),用 WTForms 默认的 validate 方法,好像只能验证本表单 self field 的内容?怎么获取其他表单的信息呢?

    3 条回复    2018-05-23 17:43:22 +08:00
    stepsame
        1
    stepsame  
       2018-05-23 10:26:20 +08:00   ❤️ 1
    self.field-a.data
    lfzyx
        2
    lfzyx  
       2018-05-23 17:40:47 +08:00
    EqualTo 了解下?
    lfzyx
        3
    lfzyx  
       2018-05-23 17:43:22 +08:00
    哦,你是要不同,那就不能用 EqualTo 了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3430 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:42 · PVG 19:42 · LAX 04:42 · JFK 07:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.