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

Unique flag not reflected in WTForms validation #58

Closed
sebdah opened this issue Jun 13, 2013 · 2 comments
Closed

Unique flag not reflected in WTForms validation #58

sebdah opened this issue Jun 13, 2013 · 2 comments
Labels
question type: enhancement Enhancement update for old feature

Comments

@sebdah
Copy link

sebdah commented Jun 13, 2013

It seems like the unique flag in MongoEngine is not reflected properly in the WTForms validation. I have a model like this (where the email is unique):

class User(database.Document):
    """ User model """
    email = database.EmailField(required=True, unique=True)
    first_name = database.StringField(required=True, max_length=30)
    last_name = database.StringField(required=True, max_length=30)
    password = database.StringField(required=True)
    is_active = database.BooleanField(default=False)
    is_authenticated = database.BooleanField(default=False)

But when I save this I get a mongoengine.errors.NotUniqueError from MongoEngine. I expected to get an error message from the form validation instead.

@lafrech
Copy link
Member

lafrech commented Feb 23, 2016

Validation takes place at field level. The form itself can't tell if the document is unique. It would need to query the DB. That's not something WTForms does.

I've been facing the same issue. For now, I've left it aside and I catch NotUniqueError in the controller, but perhaps I could setup a function to query the DB and wrap this into a custom validator.

Suggestions welcome.

@lafrech lafrech added type: enhancement Enhancement update for old feature question labels Feb 23, 2016
@wojcikstefan
Copy link
Member

Agreed w/ @lafrech that this is not something that the form validation should deal with. Catching mongoengine.errors.NotUniqueError and then responding with a validation error message sounds like a perfectly fine approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question type: enhancement Enhancement update for old feature
Projects
None yet
Development

No branches or pull requests

3 participants