Back to index

plone3  3.1.7
checkboxwidget.py
Go to the documentation of this file.
00001 from zope.app.form.browser.widget import renderElement
00002 from zope.i18nmessageid import MessageFactory
00003 from zope.i18n import translate
00004 from zope.app.form.browser import CheckBoxWidget as BaseWidget
00005 
00006 _ = MessageFactory('plone')
00007 
00008 class CheckBoxWidget(BaseWidget):
00009     """ Plone specific widget that is going to show the checkbox widget on the left of the label
00010         in order to do that we remove the title / label / required
00011     """
00012 
00013     def __init__(self, context, request):
00014         BaseWidget.__init__(self, context, request)
00015         self.required = self.__required = False
00016         self.__name = self.name
00017         self.name = ""
00018 
00019     label = property(lambda self: "")
00020 
00021     hint = property(lambda self: "")
00022 
00023     disabled = False
00024 
00025     def error(self):
00026         return ""
00027 
00028     def __call__( self ):
00029         """Render the widget to HTML."""
00030         value = self._getFormValue()
00031         html = "<label for='%s'>%s</label>\n" % (self.name , translate(super(BaseWidget,self).label, context=self.request))
00032         if self.__required:
00033             html += "<span class='fieldRequired' title='%s'>%s</span>" % (translate(_(u'title_required'), context=self.request, default='Required'), translate(_(u'label_required'), context=self.request, default='(Required)'))
00034         if super(BaseWidget, self).hint:
00035             html += "<div class='formHelp'>%s</div>" % translate(super(BaseWidget, self).hint, context=self.request)
00036         if super(BaseWidget, self).error() != '':
00037             html += "<div>%s</div>" % translate(super(BaseWidget, self).error(), context=self.request)
00038         
00039         if value == 'on':
00040             kw = {'checked': 'checked'}
00041         else:
00042             kw = {}
00043         if self.disabled:
00044             kw['disabled'] = 'disabled'
00045         return "%s  %s %s" % (
00046             renderElement(self.tag,
00047                           type='hidden',
00048                           name=self.name+".used",
00049                           id=self.name+".used",
00050                           value=""
00051                           ),
00052             renderElement(self.tag,
00053                           type=self.type,
00054                           name=self.name,
00055                           id=self.name,
00056                           cssClass=self.cssClass,
00057                           extra=self.extra,
00058                           value="on",
00059                           **kw),
00060             html
00061             )
00062 
00063 
00064 class DisabledCheckBoxWidget(CheckBoxWidget):
00065     """Simple variation of the CheckBoxWidget which renders itself disabled.
00066     """
00067     
00068     disabled = True