web/form.py
branchtls-sprint
changeset 1032 25ec009daa95
parent 1031 1a89683cb687
child 1047 21d4d5e6aa45
equal deleted inserted replaced
1031:1a89683cb687 1032:25ec009daa95
   253 # widgets ############
   253 # widgets ############
   254 
   254 
   255 class FieldWidget(object):
   255 class FieldWidget(object):
   256     needs_js = ()
   256     needs_js = ()
   257     needs_css = ()
   257     needs_css = ()
   258     
   258     setdomid = True
   259     def __init__(self, attrs=None):
   259     
       
   260     def __init__(self, attrs=None, setdomid=None):
   260         self.attrs = attrs or {}
   261         self.attrs = attrs or {}
       
   262         if setdomid is not None:
       
   263             # override class's default value
       
   264             self.setdomid = setdomid
   261 
   265 
   262     def add_media(self, form):
   266     def add_media(self, form):
   263         """adds media (CSS & JS) required by this widget"""
   267         """adds media (CSS & JS) required by this widget"""
   264         req = form.req
   268         req = form.req
   265         if self.needs_js:
   269         if self.needs_js:
   274         name = form.context[field]['name']
   278         name = form.context[field]['name']
   275         values = form.context[field]['value']
   279         values = form.context[field]['value']
   276         if not isinstance(values, (tuple, list)):
   280         if not isinstance(values, (tuple, list)):
   277             values = (values,)
   281             values = (values,)
   278         attrs = dict(self.attrs)
   282         attrs = dict(self.attrs)
   279         attrs['id'] = form.context[field]['id']
   283         if self.setdomid:
       
   284             attrs['id'] = form.context[field]['id']
   280         return name, values, attrs
   285         return name, values, attrs
   281 
   286 
   282 class Input(FieldWidget):
   287 class Input(FieldWidget):
   283     type = None
   288     type = None
   284     
   289     
   314         name, values, attrs = super(FileInput, self)._render_attrs(form, field)
   319         name, values, attrs = super(FileInput, self)._render_attrs(form, field)
   315         return name, ('',), attrs
   320         return name, ('',), attrs
   316         
   321         
   317 class HiddenInput(Input):
   322 class HiddenInput(Input):
   318     type = 'hidden'
   323     type = 'hidden'
       
   324     setdomid = False # by default, don't set id attribute on hidden input
   319     
   325     
   320 class ButtonInput(Input):
   326 class ButtonInput(Input):
   321     type = 'button'
   327     type = 'button'
   322 
   328 
   323 class TextArea(FieldWidget):
   329 class TextArea(FieldWidget):