-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature request/question] Form wise errors #55
Comments
Well, you could overwrite the form validate method to achieve this behavior but (not so classy), I also would like this feature. Something like django non_field_errors. +1 |
Is any work being done for this? A perfect use-case for this is in a login-form where you want to tell the user that the combination of the username and password field is invalid. FYI a simple fix for now is to create a subclass of class BaseForm(Form):
def __init__(self, *args, **kwargs):
self.global_errors = []
super(BaseForm, self).__init__(*args, **kwargs)
def add_global_error(self, error_msg):
self.global_errors.append(error_msg)
class SignInForm(BaseForm):
email = TextField(_('E-mail'), validators=[DataRequired()])
password = TextField(_('Password'), validators=[DataRequired()]) Usage example: form = SignInForm()
if not User.authenticate(form.email.data, form.password.data):
form.add_global_error('Invalid email or password') And then to display the error(s) in your template: <form class="{% if form.global_errors %}has-error{% endif %}">
...
{% if form.global_errors %}
<ul class="errors">
{% for error in form.global_errors %}
<li class="help-block">{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
... |
I think this is actually not needed, because you can always override the |
Warehouse (PyPI) has this: https://github.com/pypa/warehouse/blob/efc2a3fc18783d4375f878de548318d00f206924/warehouse/forms.py#L109-L116 They use the string |
On form validation I want to raise errors such as: "at least a field must be filled" which do not belong to one field, but to the form as a whole.
Would it be possible to raise have them on the form? (I'm currently using a hidden field and validating these kind of errors on it).
The text was updated successfully, but these errors were encountered: