[Twisted-web] Matt's "forms" - error when using field Integer(immutable=True)

Paul Reznicek maillists at ivsn.com
Mon Dec 12 11:23:39 MST 2005

Hi Matt,

Attached patch corrects an error when using field  Integer(immutable=True)
and form re-render because some validation failed (i.e. other required
field is empty, etc.)

Additionally, I've added some line breaks for the renderer to
make the produced HTML more readable (I needed it while debugging).

Thanks for the great package!

-------------- next part --------------
Index: widget.py 
--- widget.py	(Revision 147) 
+++ widget.py	(Arbeitskopie) 
@@ -39,7 +39,11 @@ 
     def render(self, ctx, key, args, errors): 
         if errors: 
-            value = args.get(key, [''])[0] 
+            # this correct an error when using field  Integer(immutable=True) 
+            # and form re-render because some validation failed 
+            value = args.get(key, ['']) 
+            if type(value) in (list, tuple): 
+                value = args.get(key, [''])[0] 
             value = iforms.IStringConvertible(self.original).fromType(args.get(key)) 
         if not self.showValueOnFailure: 
@@ -171,6 +175,7 @@ 
         return [ 
             T.input(type='password', name=key, id=keytocssid(ctx.key), value=values[0], class_='readonly', readonly='readonly'), 
+            T.xml('\n'), 
             T.label(for_='%s__confirm'%keytocssid(ctx.key))[' Confirm '], 
             T.input(type='password', name=key, id='%s__confirm'%keytocssid(ctx.key), value=values[1], class_='readonly', readonly='readonly') 
@@ -236,7 +241,7 @@ 
         def renderOptions(ctx, data): 
             if self.noneOption is not None: 
-                yield T.option(value=iforms.IKey(self.noneOption).key())[iforms.ILabel(self.noneOption).label()] 
+                yield T.xml('\n'), T.option(value=iforms.IKey(self.noneOption).key())[iforms.ILabel(self.noneOption).label()] 
             if data is None: 
             for item in data: 
@@ -246,7 +251,7 @@ 
                 option = T.option(value=optValue)[optLabel] 
                 if optValue == value: 
                     option = option(selected='selected') 
-                yield option 
+                yield T.xml('\n'), option 
         tag=T.select(name=key, id=keytocssid(ctx.key), data=self.options)[renderOptions] 
         if disabled: 
@@ -283,7 +288,7 @@ 
             tag = T.input(name=key, type='radio', id=cssid, value=itemKey) 
             if selected: 
                 tag = tag(checked='checked') 
-            return tag, ' ', T.label(for_=cssid)[itemLabel], T.br 
+            return tag, ' ', T.label(for_=cssid)[itemLabel], T.br, T.xml('\n') 
         def renderOptions(ctx, data): 
             # A counter to assign unique ids to each input 
@@ -324,16 +329,16 @@ 
     The default entry format is the US (month, day, year) but can be switched to 
     the more common (day, month, year) by setting the dayFirst attribute to 
     By default the widget is designed to only accept unambiguous years, i.e. 
     the user must enter 4 character dates. 
     Many people find it convenient or even necessary to allow a 2 character 
     year. This can be allowed by setting the twoCharCutoffYear attribute to an 
     integer value between 0 and 99. Anything greater than or equal to the cutoff 
     year will be considered part of the 20th century (1900 + year); anything 
     less the cutoff year is a 21st century (2000 + year) date. 
     A typical twoCharCutoffYear value is 70 (i.e. 1970). However, that value is 
     somewhat arbitrary. It's the year that time began according to the PC, but 
     it doesn't mean much to your non-techie user. 

More information about the Twisted-web mailing list