0
votes

When using the Unique() validator of wtforms_components (with Flask and SQLAlchemy), the default error message that shows up in the form is "Already exists". How can I change this error message as easy as I can change the error message of the DataRequired() validator?


The error message of the DataRequired() validator can be changed like this:

from wtforms.validators import DataRequired


class SignupForm():
    email = EmailField('Email', validators=[DataRequired('THIS IS MY NEW ERROR MESSAGE.')])

This changes the default error message from "This is required." to "THIS IS MY NEW ERROR MESSAGE." I am looking for a similarly simple and elegant solution for changing the default error message of the Unique() validator of wtforms_components


I was hoping that there is something like the following. However, it does not work:

from wtforms_components import Unique

  class SignupForm():
        email = EmailField('Email', validators=[Unique(User.email, get_session=lambda: db.session, 'THIS IS MY NEW ERROR MESSAGE.')])

The way, the validator is normally works (with the default error message) is as follows:

from wtforms_components import Unique

  class SignupForm():
        email = EmailField('Email', validators=[Unique(User.email, get_session=lambda: db.session)])

I am using the following packages:

Flask==0.10.1
Flask-SQLAlchemy==2.1
Flask-WTF==0.9.5
WTForms-Components==0.9.7

I did not find a solution in the WTForms-Alchemy documentation (http://wtforms-alchemy.readthedocs.io/en/latest/validators.html). Also, I did not find a solution in the WTForms-Components documentation (http://wtforms-components.readthedocs.io/en/latest/#unique-validator). There was no solution in this forum either.

Thanks a lot for your hints in advance.

2

2 Answers

0
votes

The source code for the Unique() function clearly shows that there is a 3rd parameter called message that you pass as the error message. (Which is what you seem to be doing.)

https://github.com/kvesteri/wtforms-alchemy/blob/master/wtforms_alchemy/validators.py

class Unique(object):
    """Checks field values unicity against specified table fields.
    :param column:
        InstrumentedAttribute object, eg. User.name, or
        Column object, eg. user.c.name, or
        a field name, eg. 'name' or
        a tuple of InstrumentedAttributes, eg. (User.name, User.email) or
        a dictionary mapping field names to InstrumentedAttributes, eg.
        {
            'name': User.name,
            'email': User.email
        }
    :param get_session:
        A function that returns a SQAlchemy Session. This parameter is not
        needed if the given model supports Flask-SQLAlchemy styled query
        parameter.
    :param message:
        The error message.
    """
    field_flags = ('unique', )

    def __init__(self, column, get_session=None, message=None):
        self.column = column
        self.message = message
        self.get_session = get_session

I'm suggesting you may have some other issue- what is the error that you are getting as well as the stack trace?

0
votes

Thanks a lot, shumuels! I had not specified the key message= and that is why it was not working. I will go through the source code on Github in the future if I don't find answers in the documentation.

from wtforms_components import Unique

  class SignupForm():
        email = EmailField('Email', validators=[Unique(User.email, get_session=lambda: db.session, message='THIS IS MY NEW ERROR MESSAGE.')])