Skip to content
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

Closed
alexef opened this issue Feb 26, 2014 · 4 comments
Closed

[feature request/question] Form wise errors #55

alexef opened this issue Feb 26, 2014 · 4 comments
Labels
Milestone

Comments

@alexef
Copy link

@alexef alexef commented Feb 26, 2014

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).

@italomaia
Copy link

@italomaia italomaia commented Jun 20, 2014

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

@jmagnusson
Copy link

@jmagnusson jmagnusson commented Mar 22, 2015

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 wtforms.form.Form:

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 %}
...
@kissgyorgy
Copy link

@kissgyorgy kissgyorgy commented Mar 14, 2016

I think this is actually not needed, because you can always override the validate method and check for field errors after calling the superclass validate method there.
For a complex example, see: http://flask.pocoo.org/snippets/64/

@davidism
Copy link
Member

@davidism davidism commented Apr 22, 2020

Warehouse (PyPI) has this: https://github.com/pypa/warehouse/blob/efc2a3fc18783d4375f878de548318d00f206924/warehouse/forms.py#L109-L116

They use the string "__all__" for the key, @azmeuk suggested None. Pinging @di because he edited the code I linked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants
You can’t perform that action at this time.