[formfields] Handle 'placeholder' attribute for text and textarea
authorChristophe de Vienne <christophe@unlish.com>
Tue, 07 Jan 2014 15:10:57 +0100
changeset 9718 927d035b6921
parent 9717 a6834e2dcc1b
child 9724 e45bf9baa7b7
[formfields] Handle 'placeholder' attribute for text and textarea See #636930. Closes #3406713.
web/formfields.py
web/formwidgets.py
--- a/web/formfields.py	Mon Apr 14 23:00:19 2014 +0200
+++ b/web/formfields.py	Tue Jan 07 15:10:57 2014 +0100
@@ -529,6 +529,7 @@
     """
     widget = fw.TextArea
     size = 45
+    placeholder = None
 
     def __init__(self, name=None, max_length=None, **kwargs):
         self.max_length = max_length # must be set before super call
@@ -547,6 +548,9 @@
         elif isinstance(self.widget, fw.TextInput):
             self.init_text_input(self.widget)
 
+        if self.placeholder:
+            self.widget.attrs.setdefault('placeholder', self.placeholder)
+
     def init_text_input(self, widget):
         if self.max_length:
             widget.attrs.setdefault('size', min(self.size, self.max_length))
@@ -557,6 +561,11 @@
             widget.attrs.setdefault('cols', 60)
             widget.attrs.setdefault('rows', 5)
 
+    def set_placeholder(self, placeholder):
+        self.placeholder = placeholder
+        if self.widget and self.placeholder:
+            self.widget.attrs.setdefault('placeholder', self.placeholder)
+
 
 class PasswordField(StringField):
     """Use this field to edit password (`Password` yams type, encoded python
--- a/web/formwidgets.py	Mon Apr 14 23:00:19 2014 +0200
+++ b/web/formwidgets.py	Tue Jan 07 15:10:57 2014 +0100
@@ -210,6 +210,8 @@
             attrs['id'] = field.dom_id(form, self.suffix)
         if self.settabindex and not 'tabindex' in attrs:
             attrs['tabindex'] = form._cw.next_tabindex()
+        if 'placeholder' in attrs:
+            attrs['placeholder'] = form._cw._(attrs['placeholder'])
         return attrs
 
     def values(self, form, field):