gpt4 book ai didi

python-3.x - 如何将 Bootstrap 验证添加到 WTForms

转载 作者:行者123 更新时间:2023-12-04 12:24:29 25 4
gpt4 key购买 nike

我正在将 WTForms 与 Flask 结合使用,我想集成 Bootstrap Form Validation对于我的表单中的错误。我有一个基本的登录表单设置,如下所示:

class LoginForm(FlaskForm):
"""Login form."""

email = EmailField(
"Email Address", validators=[DataRequired(), Email(), Length(min=6, max=40)]
)
password = PasswordField(
"Password", validators=[DataRequired()]
)

def __init__(self, *args, **kwargs):
"""Create instance."""
super(LoginForm, self).__init__(*args, **kwargs)
self.user = None

def validate(self):
"""Validate the form."""
initial_validation = super(LoginForm, self).validate()
if not initial_validation:
return False

self.user = User.query.filter_by(email=self.email.data).first()
if not self.user:
self.email.errors.append("Unknown email address!")
return False

if not self.user.check_password(self.password.data):
self.password.errors.append("Invalid password!")
return False

if not self.user.verified:
self.email.errors.append("Please verify your email address!")
return False
return True

我的 login.html 模板设置如下:
<form method="POST" action="{{ url_for('public.login') }}" role="form">
{{ form.csrf_token }}
<div class="form-group">
{{ form.email.label(class_="form-control-label") }}
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
{{ form.email(placeholder="name@example.com", class_="form-control") }}
</div>
</div>
<div class="form-group mb-4">
<div class="d-flex align-items-center justify-content-between">
<div>
{{ form.password.label(class_="form-control-label") }}
</div>
<div class="mb-2">
<a href="{{ url_for('public.recover') }}" class="small text-muted text-underline--dashed border-primary">Lost password?</a>
</div>
</div>
<div class="input-group input-group-merge">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
{{ form.password(placeholder="Password", class_="form-control") }}
<div class="input-group-append" onclick="togglePassword()">
<span class="input-group-text">
<i class="fas fa-eye"></i>
</span>
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-md-auto mt-1 mb-2" align="center">
<button type="submit" class="btn btn-sm btn-primary btn-icon rounded-pill">
<span class="btn-inner--text">Sign in</span>
<span class="btn-inner--icon"><i class="fas fa-long-arrow-alt-right"></i></span>
</button>
</div>
<div class="col-md-auto text-center mt-2">
<p class="text-secondary-dark">or</p>
</div>
<div class="col-md-auto" align="center">
<button type="button" class="btn btn-md btn-secondary btn-icon-only">
<span class="btn-inner--icon">
<i class="fab fa-google"></i>
</span>
</button>

<button type="button" class="btn btn-md btn-secondary btn-icon-only">
<span class="btn-inner--icon">
<i class="fab fa-linkedin"></i>
</span>
</button>
</div>
</div>
</form>

我想显示我使用 WTForms 验证的错误,但我不确定如何将原始表单元素的类更改为 is-invalidis-valid ,以及如何为每个错误创建标签。我研究过宏,但它们似乎也无法修改表单元素。

有人可以指出我正确的方向吗?

最佳答案

我遇到了同样的问题,我想避免使用任何第三个包(flask-bootstrap),我想出了这个简单的解决方案:

<div class="form-group">
{{ form.email.label }} <span class="text-danger">*</span>

{# her #}
{{ form.email(class="form-control" + (" is-invalid" if form.email.errors else "") + " rounded-0 shadow-none", **{"placeholder": "Your Email", "aria-describedby": "emailHelp", "autocomplete": "off"}) }}


<small id="emailHelp" class="form-text text-muted">We'll never share your data with anyone else.</small>

{% if form.email.errors %}
{% for error in form.email.errors %}
<div class="invalid-feedback">{{ error }}</div>
{% endfor %}
{% endif %}
</div>

诀窍是使用一个简单的 三元表达式结合 字符串连接 .

关于python-3.x - 如何将 Bootstrap 验证添加到 WTForms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61241168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com