我使用bootstrap / wtf.html模板来显示我的表单。我的所有表单输入字段都有wtform验证器,允许我定义自定义错误消息。不幸的是,从flask_wtf导入的RecaptchaField似乎不支持验证器(不是为了验证它,因为它是自动完成的,但是用于定义自定义错误消息)。用户不应该面对技术消息,而应该只是一个很好的通用消息。仍然是定义我的自定义消息最简单,最干净的选择 通过使用bootstrap / wtf模板?其他选项是停止使用模板渲染recaptcha字段,但如果可能的话我宁愿不这样做。
谢谢!
答案 0 :(得分:0)
创建字段时传递自定义错误消息。例如:
from flask_wtf.recaptcha import RecaptchaField, Recaptcha
class SignupForm(Form):
email = EmailField('Email')
recaptcha = RecaptchaField(validators=[Recaptcha(message="Your custom message")])
submit = InlineSubmitField('Join Now')
答案 1 :(得分:0)
from flask_wtf.recaptcha.fields import RecaptchaField
from wtforms import Form, BooleanField, StringField, PasswordField, IntegerField, validators
from flask import render_template, request, redirect, url_for, jsonify
class InquiryForm(Form):
email = StringField('email', [validators.Email(message=(u'invalid email address'))])
recaptcha = RecaptchaField()
@app.route('/inquiry', methods=['POST','GET'])
def inquiry():
form = InquiryForm(request.form)
if request.method == 'POST' and form.validate():
return jsonify({'success' : 'Message sent'})
else:
# request.form.get('g-recaptcha-response')
# google recaptcha v2 validate return
if request.form.get('g-recaptcha-response') and form.recaptcha.errors:
form.recaptcha.errors.append("System has detected possible suspicious activity, please refresh this page.")
form.recaptcha.errors.reverse()
elif form.recaptcha.errors:
form.recaptcha.errors.append("Need to validate recaptcha")
form.recaptcha.errors.reverse()
return jsonify(form.errors)
这是我添加自定义Recaptcha错误消息的方式