验证表单后,wtforms会引发验证错误 [英] wtforms raise a validation error after the form is validated
本文介绍了验证表单后,wtforms会引发验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个收集信用卡信息的注册表.工作流程如下:
I have a registration form that collects credit card info. The workflow is as follows:
- 用户通过条带输入注册数据和卡数据.
- 该表格已针对注册数据进行了验证.
- 如果该表格有效,则将处理付款.
- 如果付款成功,那么一切都很好,用户已注册并继续前进.
- 如果付款失败,我希望能够在表单的隐藏字段上引发验证错误.有可能吗?
这是一个表单提交代码:
Here's a the form submission code:
def register():
form = RegistrationForm()
if form.validate_on_submit():
user = User(
[...]
)
db.session.add(user)
#Charge
amount = 10000
customer = stripe.Customer.create(
email=job.company_email,
card=request.form['stripeToken']
)
try:
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Registration payment'
)
except StripeError as e:
***I want to raise a form validation error here if possible.***
db.session.commit()
return redirect(url_for('home'))
return render_template('register.html', form=form)
推荐答案
我通过将错误手动添加到我想要的字段中来解决了这个问题.
I solved it by manually appending errors to the field i wanted.
看起来像这样
try:
[...]
except StripeError as e:
form.payment.errors.append('the error message')
else:
db.session.commit()
return redirect(url_for('home'))
这篇关于验证表单后,wtforms会引发验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文