I’ve been testing this refactor (https://github.com/web2py/py4web/pull/1001) because the feature that allows widgets to have a read-only version (def make_readonly(self, value)) seems extremely useful.
I haven’t evaluated the widget registration mechanism yet.
However, I’ve run into major issues with my custom widgets, and at the moment it doesn’t seem easy to make them work with this change.
Before:
form_style.widgets['select_field'] = WidgetTomSelect(create=False)Now:
form_style.widgets['select_field'] = WidgetTomSelectWith the new approach, I can no longer pass parameters to the widget constructor.
This makes it impossible to configure widgets that need runtime options.
For example, my WidgetTomSelect:
class WidgetTomSelect: def __init__(self, create: bool = False): self.create = create def make(self, field, value, error, title, placeholder="", readonly=False): if not hasattr(field.requires, 'options'): raise ValueError('This field does not support WidgetTomSelect') widget = SelectWidget().make(field, value, error, title, placeholder, readonly) widget['_class'] = (widget.attributes.get('_class', '') + ' tomselect').strip() return widgetWith the refactor, it is now impossible to instantiate SelectWidget directly, so this pattern no longer works.
In:
class OldWidgetCompat(Widget): """Handles custom widgets from the older style, which have a .make(self, field, value, error, title, placeholder="", readonly=False) and don't inherit from Widget""" def __init__(self, old_widget, field, form_style, vars, error=None): super().__init__(field, form_style, vars, error) self.old_widget = old_widgetIt seems that old_widget should actually be instantiated:
self.old_widget = old_widget()Otherwise, old-style widgets that expect instance state cannot work correctly.
Finally, the standard py4web widgets are now defined like this:
class InputTypeWidget(Widget, MakeReadonlyMixin):But MakeReadonlyMixin does not seem to have any effect in this order.
Should this instead be:
so that the mixin methods properly override or extend the base behavior?
Overall, I really like the idea behind make_readonly, but at least in my case, migrating existing custom widgets has been quite difficult with the current design.
Thanks for the work on this — I hope this feedback is useful.