V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · May 23, 2018 · 1968 views
    This topic created in 2904 days ago, the information mentioned may be changed or developed.

    在验证 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 replies    2018-05-23 17:43:22 +08:00
    stepsame
        1
    stepsame  
       May 23, 2018   ❤️ 1
    self.field-a.data
    lfzyx
        2
    lfzyx  
       May 23, 2018
    EqualTo 了解下?
    lfzyx
        3
    lfzyx  
       May 23, 2018
    哦,你是要不同,那就不能用 EqualTo 了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3498 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 00:10 · PVG 08:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.