Opened 7 years ago
Closed 7 years ago
#29189 closed New feature (wontfix)
Allow customizing the CSS classes of the <tr>, <ul>, and <p> in Form.as_table(), as_ul(), and_p()
Reported by: | James Pic | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 2.0 |
Severity: | Normal | Keywords: | |
Cc: | James Pic | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Currently, Form._html_output() calls BoundField.css_classes() to get the list of classes to apply on the field container, such as error_css_class or required_css_class.
My objective is something like :
form['myfield'].extra_css_classes = 'hide'
(Setting the class in JS too, but this causes a flash of course)
Could we perhaps allow adding extra css classes to BoundField ?
Currently I have this little hack which works but isn't doing anything that's supposed to be supported so please bare with me, it's just here to serve as an example and attempt to illustrate what I would like to get rid of in my own code:
class BoundField(forms.BoundField): """BoundField override for facond Form.""" def css_classes(self): """Allow setting extra_css_classes.""" return super(BoundField, self).css_classes( getattr(self, 'extra_css_classes', '')) class Form(forms.Form): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for name, field in self.fields.items(): # not sure what it breaks to instanciate BoundFields here # rather than in __getitem__ self._bound_fields_cache[name] = BoundField(self, field, name)
Thanks in advance for your feedback, I can submit a patch if you want.
Change History (7)
comment:1 by , 7 years ago
Cc: | added |
---|---|
Description: | modified (diff) |
Type: | Uncategorized → New feature |
comment:2 by , 7 years ago
comment:3 by , 7 years ago
Maybe we can expose BoundField to the user, and delegate the form field container rendering to it ?
comment:4 by , 7 years ago
Could you elaborate on your proposal? I'm not sure exactly what it means.
comment:5 by , 7 years ago
I just don't understand how to properly override how a form field container renders in Django ... i hoped the new forms rendering API would help but i don't understand how ... i see many external libraries do this ... but we should probably not invest in this as there's currently a second breath in django-adapters let's see if that's a better way to spend our time
best regards friend
comment:6 by , 7 years ago
We can try to make a proposal in Django but currently in adapters the proposal is:
# Time to show off for some user love ! assert a.adapters.add('elementui.Form').steps.render().payload.rendered == '<an awesome form>' # So yeah, this kind of presentational adapters will love visiting a's map # and add()'s adapters the see fit ! assert a.adapters.add('googlemdc.Form').steps.render().payload.rendered == '<an awesome form>' # Let's just make an HTTP response ! assert a.adapters.add('googlemdc.Form').add('django.ProcessFormResponse').steps.process().payload.response
comment:7 by , 7 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Summary: | BoundField: extra css classes → Allow customizing the CSS classes of the <tr>, <ul>, and <p> in Form.as_table(), as_ul(), and_p() |
It might be that #16922 (template based form rendering) would allow this. Right now, I don't see a good way to implement it and the general recommendation is not to use as_table()
, as_ul()
, and as_p()
when you have more advanced rendering requirements.
My initial reaction is that the
BoundField.css_classes()
method, added in 92803205cbcaaee16ac0eb724c45019a9d896aac (#3512), is somewhat problematic since it's only used by theas_p()
,as_ul()
, andas_table()
methods. Adding aBoundField
attribute likeextra_css_classes
that's only respected there doesn't seem good. Perhaps with some more thinking we can come up with a more elegant solution.