Here's the patch. I adds an onrender property to Field which is called from 
the widget method of all default widgets.

On Saturday, August 24, 2013 6:42:49 PM UTC-5, mr.freeze wrote:
>
> Ideally, I wouldn't set the class via a script, that was just a hack.  I 
> would like to alter the field after its default widget is rendered. I have 
> a patch that allows you to do this. Thoughts?:
> def add_required(elm):
>     elm['_class'] += ' required'
> db.mytable.myfield.onrender = add_required
>
>
>
> On Saturday, August 24, 2013 6:22:22 PM UTC-5, Massimo Di Pierro wrote:
>>
>> Nice idea. But why put it in a comment?
>>
>> script = ''
>> for table in db:
>>     for field in table:
>>         if field.required:
>>             script += 'jQuery("#%s_%s").attr("required","")' % (
>> field._tablename,field.name)
>>
>> You can then cache the script and place in the layout.
>>
>> Massimo
>>
>>
>>
>> On Saturday, 24 August 2013 17:37:48 UTC-5, mr.freeze wrote:
>>>
>>> I want to take advantage of bootstrap's form validation classes so I 
>>> need to add a class to the field. Too bad there's not a hook into when a 
>>> Field's widget is rendered so you can manipulate it. Would you take a patch 
>>> for that?  I can work around it with your method in the meantime:
>>> for t in db.tables:
>>>     for f in db[t].fields:
>>>         if db[t][f].required:
>>>             db[t][f].comment = SCRIPT(
>>> 'jQuery("#%s_%s").attr("required","")' % (t,f))
>>>
>>>
>>> On Saturday, August 24, 2013 4:29:47 PM UTC-5, Massimo Di Pierro wrote:
>>>>
>>>> You could use something like:
>>>>
>>>>     db.table.field.comment = 'required'
>>>>
>>>> or 
>>>>
>>>> for field in db.table:
>>>>    if field.required:
>>>>        field.comment='required'
>>>>
>>>>
>>>> On Saturday, 24 August 2013 09:36:16 UTC-5, mr.freeze wrote:
>>>>>
>>>>> I want to show users which fields are required *before* they submit a 
>>>>> form. Before I reinvent the wheel, is there a mechanism for adding a 
>>>>> "required" class to fields that have IS_NOT_EMPTY or required=True 
>>>>> already 
>>>>> built into web2py?
>>>>>
>>>>> Thanks,
>>>>> Nathan
>>>>>
>>>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
Index: gluon/sqlhtml.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- gluon/sqlhtml.py	(revision 301d44914787749fdd9ec012de67bae1b4fc0756)
+++ gluon/sqlhtml.py	(revision )
@@ -169,10 +169,11 @@
             value=(not value is None and str(value)) or '',
         )
         attr = cls._attributes(field, default, **attributes)
+        inp = INPUT(**attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
-        return INPUT(**attr)
 
-
 class IntegerWidget(StringWidget):
     _class = 'integer'
 
@@ -208,7 +209,9 @@
 
         default = dict(value=value)
         attr = cls._attributes(field, default, **attributes)
-        return TEXTAREA(**attr)
+        inp = TEXTAREA(**attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 class JSONWidget(FormWidget):
     _class = 'json'
@@ -225,7 +228,9 @@
                 value = serializers.json(value)
         default = dict(value=value)
         attr = cls._attributes(field, default, **attributes)
-        return TEXTAREA(**attr)
+        inp = TEXTAREA(**attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 class BooleanWidget(FormWidget):
     _class = 'boolean'
@@ -241,7 +246,9 @@
         default = dict(_type='checkbox', value=value)
         attr = cls._attributes(field, default,
                                **attributes)
-        return INPUT(**attr)
+        inp = INPUT(**attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 
 class OptionsWidget(FormWidget):
@@ -277,7 +284,9 @@
                 raise SyntaxError(
                     'widget cannot determine options of %s' % field)
         opts = [OPTION(v, _value=k) for (k, v) in options]
-        return SELECT(*opts, **attr)
+        inp = SELECT(*opts, **attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 
 class ListWidget(StringWidget):
@@ -300,7 +309,9 @@
         attributes['_id'] = _id + '_grow_input'
         attributes['_style'] = 'list-style:none'
         attributes['_class'] = 'w2p_list'
-        return TAG[''](UL(*items, **attributes))
+        inp = TAG[''](UL(*items, **attributes))
+        if field.onrender: field.onrender(inp)
+        return inp
 
 
 class MultipleOptionsWidget(OptionsWidget):
@@ -382,7 +393,9 @@
 
         if opts:
             opts[-1][0][0]['hideerror'] = False
-        return parent(*opts, **attr)
+        inp = parent(*opts, **attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 
 class CheckboxesWidget(OptionsWidget):
@@ -453,7 +466,9 @@
                       _disabled="disabled",
                       _name=field.name,
                       hideerror=False))
-        return parent(*opts, **attr)
+        inp = parent(*opts, **attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 
 class PasswordWidget(FormWidget):
@@ -485,8 +500,9 @@
         if is_strong:
             attr['_data-w2p_entropy'] = is_strong[0].entropy if is_strong[0].entropy else "null"
         # end entropy check
-        output = INPUT(**attr)
-        return output
+        inp = INPUT(**attr)
+        if field.onrender: field.onrender(inp)
+        return inp
 
 
 class UploadWidget(FormWidget):
@@ -547,6 +563,7 @@
                                A(cls.GENERIC_DESCRIPTION, _href=url),
                                ']', _style='white-space:nowrap'),
                           br, image)
+        if field.onrender: field.onrender(inp)
         return inp
 
     @classmethod
Index: gluon/dal.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- gluon/dal.py	(revision 301d44914787749fdd9ec012de67bae1b4fc0756)
+++ gluon/dal.py	(revision )
@@ -9410,6 +9410,7 @@
         required=False,
         requires=DEFAULT,
         ondelete='CASCADE',
+        onrender=None,
         notnull=False,
         unique=False,
         uploadfield=True,
@@ -9448,6 +9449,7 @@
         self.default = default if default!=DEFAULT else (update or None)
         self.required = required  # is this field required
         self.ondelete = ondelete.upper()  # this is for reference fields only
+        self.onrender = onrender
         self.notnull = notnull
         self.unique = unique
         self.uploadfield = uploadfield

Reply via email to