For an application I need the ability to set the class of a form
element depending on the results of the validation (to, e.g., use a
red background on an input field if the validation fails).
I've looked a little bit around in the form.py source, but found no
obvious way how to do this with the current code. While it is possible
to pass a class_ to the constructor, this can only be set at runtime.
Therefore, if class_ should be set to the result of the validation
this would require two instantiations of the form: Once for the
validation, and a second time to set class_ based on the results of
validation.
So to fix this issue there would be two options:
1) The more generic option: Add a method that allows to set
attrs['class'] on an existing form so that the calling code can set
the class according to the results of the validation.
2) Set the class directly in form.py, based on the results of the
validation. E.g., with the following code:
---------- 8< ---------- 8< ---------- 8< ---------- 8< ----------
--- /usr/lib/python2.6/site-packages/web/form.py 2010-03-20
18:40:07.000000000 +0100
+++ web/form.py 2010-06-13 21:20:25.000000000 +0200
@@ -120,6 +120,7 @@
self.pre = attrs.pop('pre', "")
self.post = attrs.pop('post', "")
self.note = None
+ self.css = attrs.pop('css', False)
self.id = attrs.setdefault('id', self.get_default_id())
@@ -156,6 +157,11 @@
attrs['type'] = self.get_type()
if self.value is not None:
attrs['value'] = self.value
+ if self.css:
+ if self.validate(self.value):
+ attrs['class'] = 'correct'
+ else:
+ attrs['class'] = 'error'
attrs['name'] = self.name
return '<input %s/>' % attrs
---------- 8< ---------- 8< ---------- 8< ---------- 8< ----------
Is there any chance to get a code similar to options 1) or 2) merged
into web.py?
- Guenther
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.