[feature request/question] Form wise errors #55

Open
alexef opened this Issue Feb 26, 2014 · 3 comments

Comments

5 participants
@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

This comment has been minimized.

Show comment
Hide comment
@italomaia

italomaia 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

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

This comment has been minimized.

Show comment
Hide comment
@jmagnusson

jmagnusson 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 %}
...

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

This comment has been minimized.

Show comment
Hide comment
@kissgyorgy

kissgyorgy 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/

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/

@3noch 3noch referenced this issue in level12/keg-elements Sep 21, 2016

Closed

Support form-level errors #52

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