Skip to content

Big form widgets refactor to allow user-defined widgets, added number widgets #991

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

laundmo
Copy link
Contributor

@laundmo laundmo commented Aug 4, 2025

I was quite unsatisfied with the stats of FormStyleFactory and Form widgets. Especially when it comes to extending/overwriting them, the options were sparse - basically limited to overwriting them per field.

This PR is a pretty big refactor for Widgets and how they work.

  • Widgets themselves define what types they apply to, or even more complex matches if no direct type match was found.
  • Most Widgets should implement 1-2 methods now, one for an editable version and another for a readonly version
    • The readonly parameter of widget.make() wasn't used before, instead a default readonly formatting was applied to basically any field.
  • While Widget.make() still exists, its primarily called by form_html which is what creates the div/label structure used for comments, errors, etc. This allows user-defined widgets to change where in the HTML comments, errors, labels etc. appear.
  • The Widget class stores most of the context information and non-html widget structure, and updates it when necessary. Widget implementations call self.field, self.vars etc. when they need this information. This makes it far easier to work with custom logic compared to all of this logic being in FormStyleFactory.
  • I've tried to keep compatibility with the field.widget and formstyle.widgets options, which is done through implementing custom Widget subclasses to handle those.

Since this is a big and somewhat opinionated refactor, I haven't written documentation, exhaustive comments, and tests yet. Before I put that work in I'd want to know whether there's any chance this might be merged.

@classmethod
def matches(cls, field: Field) -> bool:
"Checks if this widget can be used for the field"
return cls.type_name == field.type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice field.type is not always a string, if can be a SQLCustomType object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants