Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #308 (closed enhancement: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] Allow widgets to be disabled on forms

Reported by: jpaulofarias at gmail dot com Owned by: anonymous
Priority: normal Milestone: 0.9
Component: TG Widgets Version:
Severity: normal Keywords:
Cc:

Description

This patch will allow widgets to be enabled/disabled on forms.

Pretty simple, inlined bellow.

Index: turbogears/widgets/base.py
===================================================================
--- turbogears/widgets/base.py	(revision 421)
+++ turbogears/widgets/base.py	(working copy)
@@ -42,10 +42,11 @@
     sample_code = 'widget = %(name)s("sample")'
     sample_value = 'Sample'
     hidden = None
+    disabled = None
     file_upload = False
 
     def __init__(self, name=None, template=None, label=None,
-                 labeltext=None, default=None, validator=None, hidden=None, attrs={}):
+                 labeltext=None, default=None, validator=None, hidden=None, disabled=None, attrs={}):
         if name:
             self.name = name
         if template:
@@ -62,7 +63,8 @@
             self.default = default
         if validator:
             self.validator = validator
-
+        if disabled:
+            self.disabled = disabled
         self.attrs = attrs
 
     def create_dict(self, value, input_values, error,
@@ -469,7 +471,8 @@
                 JSLink(static, "calendar-setup.js")]
     template = """
     <div xmlns:py="http://purl.org/kid/ns#" py:strip="True">
-    <input type="text" id="${widget.name}" name="${widget.name}" value="${widget_value}" class="date_field"/>
+    <input type="text" id="${widget.name}" name="${widget.name}" 
+        value="${hasattr(widget_value, 'strftime') and widget_value.strftime('%m/%d/%Y') or widget_value}" class="date_field"/>
     <button id="${widget.name}-trigger" class="date_field_button">Choose</button>
     <script type="text/javascript">
     Calendar.setup(
Index: turbogears/widgets/forms.py
===================================================================
--- turbogears/widgets/forms.py	(revision 421)
+++ turbogears/widgets/forms.py	(working copy)
@@ -22,7 +22,7 @@
        ${widget.insert(getattr(self.widget_value, widget.name, None), input_values, widget_error.get(widget.name, None))}
     </div>
     <table border="0">
-    <tr py:for="widget in widgets" py:if="not widget.hidden">
+    <tr py:for="widget in widgets" py:if="not widget.hidden and not widget.disabled">
             <td>
                 <span py:if="widget.label and widget.labeltext" 
                       py:replace="widget.label.insert(widget.labeltext)"/>

Change History

comment:1 Changed 13 years ago by kevin

I just sent an email message for clarification. As written, this wouldn't be very useful because the disabled state can't change from request to request (due to threadsafety issues.)

comment:2 Changed 13 years ago by jpaulofarias at gmail dot com

  • Type changed from defect to enhancement

I think the other way is to put a condition attribute that is evaluated on render time, so that the field row on table form would have a py:if="eval(condition)" or something like that.

Hope it helps.

comment:3 Changed 13 years ago by Luca <luca@…>

  • Component changed from TurboGears to Widgets
  • Summary changed from Patch to allow widgets to be disabled on forms to [PATCH] Allow widgets to be disabled on forms

comment:4 Changed 13 years ago by jpaulofarias at gmail dot com

New patch to allow disabling widgets per request.

To use it one need to create a list with the name of the disabled widgets and pass it as 'disabled_widgets' parameter to form.insert() like this:

<span py:replace="my_form.insert(disabled_widgets=['widget1', 'widget2'])"/>

Hope this time there is no threading issues.

Index: turbogears/widgets/forms.py
===================================================================
--- turbogears/widgets/forms.py	(revision 438)
+++ turbogears/widgets/forms.py	(working copy)
@@ -133,11 +133,12 @@
 <form xmlns:py="http://purl.org/kid/ns#" name="${widget.name}" 
       action="${getattr(self, 'action', None)}" method="${getattr(self, 'method', 'post')}"
       enctype="${getattr(self, 'enctype', None)}">
-    <div py:for="widget in widgets" py:if="widget.hidden" py:strip="True">
+    <?python disabled_widgets = locals().get('disabled_widgets', []) ?>
+    <div py:for="widget in widgets" py:if="widget.hidden and widget.name not in disabled_widgets" py:strip="True">
        ${widget.insert(getattr(self.widget_value, widget.name, None), input_values, widget_error.get(widget.name, None))}
     </div>
     <table border="0">
-    <tr py:for="widget in widgets" py:if="not widget.hidden">
+    <tr py:for="widget in widgets" py:if="not widget.hidden and widget.name not in disabled_widgets">
             <td>
                 <span py:if="widget.label and widget.labeltext" 
                       py:replace="widget.label.insert(widget.labeltext)"/>

comment:5 Changed 13 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed

I committed a form of this in [544]. In my implementation I use the poorly documented value_of function rather than locals().get.

Note: See TracTickets for help on using tickets.