-
Notifications
You must be signed in to change notification settings - Fork 17
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
Supply context for forms #112
Comments
Since there is no way to define an appropriate query in a base class, I propose this be part of the convention around form construction. Update: This particular issue was addressed in #145. |
Python 3.x's def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if {'edit_obj', 'edit_model', 'edit_parent', 'edit_id'} & set(cls.__expects__):
raise TypeError("This form has __expects__ parameters that are reserved by the base form")
if set(cls.__dict__.keys()) & set(cls.__expects__):
raise TypeError("This form has __expects__ parameters that clash with field names") However:
|
Since we now have Py3 support and intend to upgrade our apps, the |
There is no validation of
Given the non-existent use so far, we can take the Py3-only solution. In addition, |
Forms often require additional context to be able to do validations correctly.
For example, Lastuser's profile form needs to validate the provided username against usernames already taken in the database. This requires two pieces of context: the User model, and the current user's record so we exclude that record when checking if the username is already taken. This means context cannot be hardcoded into the form. The form may have a permanent reference to the User model, but needs the view to tell it what user's record is being edited.
WTForms provides for one crucial piece of context: the object being edited as the
obj
parameter to the form constructor. We've extended this in Baseframe's Form class to also expectmodel
andparent
. However, forms may sometimes require even more context. Our approach has been to stick these attributes into the form in the view, and to expect them to be present in the form's validators. Sometimes it is the other way: the validator sticks in a new attribute and the view reads it (example in Hasjob: validator and view).This approach makes it hard to reuse a form without being aware of such expectations, and so this calls for a formal API. I propose adding two attributes to the form class,
__expects__
and__returns__
, both of which contain a list of strings naming (a) parameters that must be present in the constructor failing which an exception is raised, and (b) attributes guaranteed to be present on the form instance after validation (default valueNone
).The text was updated successfully, but these errors were encountered: