fix StringField/TextField handling (XXX remove one of them?, ensure textarea has rows/cols attributes) tls-sprint
authorsylvain.thenault@logilab.fr
Thu, 30 Apr 2009 01:20:59 +0200
branchtls-sprint
changeset 1557 d2d5428c7a38
parent 1556 b64a0fe1fc86
child 1558 f63d090eb34a
fix StringField/TextField handling (XXX remove one of them?, ensure textarea has rows/cols attributes)
web/formfields.py
web/formwidgets.py
--- a/web/formfields.py	Thu Apr 30 01:19:50 2009 +0200
+++ b/web/formfields.py	Thu Apr 30 01:20:59 2009 +0200
@@ -180,14 +180,13 @@
     def __init__(self, max_length=None, **kwargs):
         super(StringField, self).__init__(**kwargs)
         self.max_length = max_length
+        if max_length < 513 and isinstance(self.widget, TextArea):
+            self.widget.attrs['cols'], self.widget.attrs['rows'] = 60, 5
 
 
-class TextField(Field):
+class TextField(StringField):
+    """XXX string field not enough?"""
     widget = TextArea
-    def __init__(self, rows=10, cols=80, **kwargs):
-        super(TextField, self).__init__(**kwargs)
-        self.rows = rows
-        self.cols = cols
 
 
 class RichTextField(TextField):
@@ -424,7 +423,7 @@
         if isinstance(cstr, SizeConstraint) and cstr.max is not None:
             if cstr.max > 257:
                 rows_cols_from_constraint(cstr, kwargs)
-                field = TextField(**kwargs)
+                field = TextField(max_length=cstr.max, **kwargs)
             else:
                 field = StringField(max_length=cstr.max, **kwargs)
     return field or TextField(**kwargs)
--- a/web/formwidgets.py	Thu Apr 30 01:19:50 2009 +0200
+++ b/web/formwidgets.py	Thu Apr 30 01:20:59 2009 +0200
@@ -137,6 +137,8 @@
     def render(self, form, field):
         name, values, attrs = self._render_attrs(form, field)
         attrs.setdefault('onkeypress', 'autogrow(this)')
+        attrs.setdefault('cols', 80)
+        attrs.setdefault('rows', 20)
         if not values:
             value = u''
         elif len(values) == 1: