FormAlchemy / formalchemy

MIT License
81 stars 29 forks source link

Fieldset.validate raises KeyError when some fields are missing #8

Closed fandre90 closed 13 years ago

fandre90 commented 13 years ago

The _validate_method of Fielset raises KeyError when some fields are missing in submitted data, whereas we would expect it to simply return false.

For example, if you have the following model :

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    email = Column(String, unique=True, nullable=False)

and the following submission validation code :

if fs.validate():
    fs.sync()
    #  Other stuff here

If you submit the following data, validate returns true :

{'email': 'gpasgrimaud@bearstech.com'}

but if you submit the following data, validate raises a KeyError exception :

{'dumbass': 'gpasgrimaud@bearstech.com'}
KeyError: u"User--email not found in SimpleMultiDict([('User--dumbass', u'gpasgrimaud@bearstech.com')])"

In this case, we would simply expect validate to return false and populate the errors dictionary accordingly.