WTF-Forms的EqualTo验证器没有给出错误消息



我有一个带有密码和确认密码字段的注册表单。当提交表单并且密码不匹配时,它不会返回任何错误消息。注册本身可以工作,如果显示密码不匹配的错误消息就好了。我看到其他人也有同样的问题,但它总是被认为是代码中的其他东西(也可能是我的情况)。这是我的表单类:

class UserForm(FlaskForm):
username = StringField("Enter Username:", validators=[InputRequired()])
email = EmailField("Enter Email:", validators=[InputRequired()])
#password = PasswordField("Enter Password:", validators=[InputRequired()])
password_hash = PasswordField("Enter Password:", validators=[InputRequired(), EqualTo('password_hash2', message="Passwords Must Match!")])
password_hash2 = PasswordField("Confirm Password:", validators=[InputRequired()])
currency = SelectField("Choose a Currency:", choices=[("USD"),("EUR"),("NOK")])
profile_pic = FileField("Profile Pic")
submit = SubmitField("Submit")

这是路由:

@app.route("/user/add", methods=['GET', 'POST'])
def add_user():
username = None
form = UserForm()
if form.validate_on_submit():
user_email = Users.query.filter_by(email=form.email.data).first()
user_username = Users.query.filter_by(username=form.username.data).first()
if user_email is None and user_username is None:
hashed_pw = generate_password_hash(form.password_hash.data, "sha256")
user = Users(username=form.username.data, email=form.email.data, password_hash=hashed_pw, currency=form.currency.data)  #, password=form.password.data)
db.session.add(user)
db.session.commit()
elif user_email is None:
flash("Username Taken")
return redirect("/user/add")
elif user_username is None:
flash("Email already in use")
return redirect("/user/add")
elif user_email is not None and user_username is not None:
flash("Email and Username Taken")
return redirect("/user/add")
username = form.username.data
form.username.data = ''
form.email.data = ''
form.password_hash.data = ''
#form.password.data = ''
flash("User Added Successfully - " + username)
our_users = Users.query.order_by(Users.date_added)
return render_template("signup.html", form=form, username=username, our_users=our_users)

html代码:

{% extends "mal.html" %}
{% block innhold %}
{% if username %}
{% for message in get_flashed_messages() %}
<br>
<div class="alert alert-primary alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
<h3>You may now log in to the website</h3>
<br>
<a href=/login>Click here to login</a>
<br>
{% else %}
{% for message in get_flashed_messages() %}
<br>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{% endfor %}
<br>
<h1>Sign Up</h1>
<h2>Sign Up Form:</h2>
<br>
<div class="shadow p-3 mb-5 bg-body rounded">
<form method="POST">
{{ form.hidden_tag() }}
{{ form.email.label() }}
<br>
{{ form.email(placeholder="example@gmail.com", class="form-control") }}
<br>
{{ form.username.label() }}
<br>
{{ form.username(class="form-control") }}
<br>
{{ form.password_hash.label() }}
<br>
{{ form.password_hash(class="form-control") }}
<div id="passwordHelpBlock" class="form-text">
Your password should be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
</div>
<br>
{{ form.password_hash2.label() }}
<br>
{{ form.password_hash2(class="form-control") }}
<br>
{{ form.currency.label(class="d-none") }}
{{ form.currency(class="form-control d-none") }}
{{ form.submit(class="btn btn-success") }}
<br><br>
<small><a href=/login>Already have an account? Login here!</a></small>
<br>
</form>
</div>
<br><br><br>
{% endif %}
{% endblock %}

我希望有人能找出这个问题或找到一个不同的方法来解决它比EqualTo,谢谢:)

问题是您没有显示错误消息,即使它们在后台工作。使用这样的块来获取和显示指定字段的任何错误消息。

{% for error in form.your_form_field.errors %}
{{ error }}
{% endfor %}

相关内容

最新更新