I see lots of apps using Form objects to validate data and then passing the data to the model, while putting absolutely no validation in the model. I feel it’s better to put core validation in the model itself (e.g., no users under the age of 18, ever) to be run regardless of the context. In other words, I don’t care how the user is being created (whether through web ui or command line), the core rules should always apply.
I’m using SQLAlchemy (within a Pyramid application), and I would like to define my core validation rules within the model in a way that my forms (WTForms) always respect the core rules defined in the model so that all data is consistent.
Is anybody else already doing this, or something similar?
Something similar to this php solution.
SQLAlchemy allows you to register listeners, which are invoked when certain events occur, for example you can register an event to be triggered when a model’s field is modified. From SQLAlchemy documentation:
So, as you see, you can either modify or reject values for certain field at the model level.
I’m not sure if your form library integrates with this feature, but it definitely should not be too difficult to roll your own solution.