Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Fixing a bug in mongorest with optional email fields
Browse files Browse the repository at this point in the history
i
If you have an email field in a document that is optional but are using a wtform for the Resource validation, mongoengine will raise an error because
wtforms returns '' instead of None for an empty email field. This doesn't pass validation of mongoengine.
  • Loading branch information
lucasvo committed Aug 23, 2012
1 parent dbab6d9 commit cd4e965
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion flask_mongorest/resources.py
Expand Up @@ -5,6 +5,7 @@
from mongoengine.fields import EmbeddedDocumentField, ListField, ReferenceField, DateTimeField
from flask.ext.mongorest.exceptions import ValidationError
import dateutil.parser
from flask.ext.wtf import validators

class ResourceMeta(type):
def __init__(cls, name, bases, classdict):
Expand Down Expand Up @@ -161,9 +162,21 @@ def json_to_form_data(prefix, json_data):
data = MultiDict(json_data)
form = self.form(data, csrf_enabled=False)

# We have to dynamically remove fields from the form, if the POST object does not contain these keys and the Optional validator
delfields = []
for key, value in form._fields.iteritems():
required = True
for validator in form._fields[key].validators:
if validator.__class__ == validators.optional().__class__:
required = False
if not key in data and key != 'csrf_token' and required == False:
delfields.append(key)

for d in delfields:
del form[d]

if not form.validate():
raise ValidationError({'field-errors': form.errors})

self.data = form.data

def get_queryset(self):
Expand Down

0 comments on commit cd4e965

Please sign in to comment.