web/formwidgets.py
changeset 4156 1bbb0ee42c8e
parent 4023 eae23c40627a
child 4159 6b2b20c73d59
equal deleted inserted replaced
4155:80cc9c6ddcf0 4156:1bbb0ee42c8e
    57         values = form.context[field]['value']
    57         values = form.context[field]['value']
    58         if not isinstance(values, (tuple, list)):
    58         if not isinstance(values, (tuple, list)):
    59             values = (values,)
    59             values = (values,)
    60         attrs = dict(self.attrs)
    60         attrs = dict(self.attrs)
    61         if self.setdomid:
    61         if self.setdomid:
    62             attrs['id'] = form.context[field]['id']
    62             attrs['id'] = field.dom_id(form)
    63         if self.settabindex and not 'tabindex' in attrs:
    63         if self.settabindex and not 'tabindex' in attrs:
    64             attrs['tabindex'] = form._cw.next_tabindex()
    64             attrs['tabindex'] = form._cw.next_tabindex()
    65         return name, values, attrs
    65         return name, values, attrs
    66 
    66 
    67     def process_field_data(self, form, field):
    67     def process_field_data(self, form, field):
    81         self.add_media(form)
    81         self.add_media(form)
    82         name, values, attrs = self._render_attrs(form, field)
    82         name, values, attrs = self._render_attrs(form, field)
    83         # ensure something is rendered
    83         # ensure something is rendered
    84         if not values:
    84         if not values:
    85             values = (INTERNAL_FIELD_VALUE,)
    85             values = (INTERNAL_FIELD_VALUE,)
    86         inputs = [tags.input(name=name, value=value, type=self.type, **attrs)
    86         inputs = [tags.input(name=field.input_name(form), type=self.type,
       
    87                              value=value, **attrs)
    87                   for value in values]
    88                   for value in values]
    88         return u'\n'.join(inputs)
    89         return u'\n'.join(inputs)
    89 
    90 
    90 
    91 
    91 # basic html widgets ###########################################################
    92 # basic html widgets ###########################################################
   104     def render(self, form, field, renderer):
   105     def render(self, form, field, renderer):
   105         self.add_media(form)
   106         self.add_media(form)
   106         name, values, attrs = self._render_attrs(form, field)
   107         name, values, attrs = self._render_attrs(form, field)
   107         assert len(values) == 1
   108         assert len(values) == 1
   108         id = attrs.pop('id')
   109         id = attrs.pop('id')
   109         try:
   110         inputs = [tags.input(name=field.input_name(form),
   110             confirmname = '%s-confirm:%s' % tuple(name.rsplit(':', 1))
   111                              value=values[0], type=self.type, id=id, **attrs),
   111         except TypeError:
       
   112             confirmname = '%s-confirm' % name
       
   113         inputs = [tags.input(name=name, value=values[0], type=self.type, id=id,
       
   114                              **attrs),
       
   115                   '<br/>',
   112                   '<br/>',
   116                   tags.input(name=confirmname, value=values[0], type=self.type,
   113                   tags.input(name=field.input_name(form, '-confirm'),
   117                              **attrs),
   114                              value=values[0], type=self.type, **attrs),
   118                   '&#160;', tags.span(form._cw._('confirm password'),
   115                   '&#160;', tags.span(form._cw._('confirm password'),
   119                                       **{'class': 'emphasis'})]
   116                                       **{'class': 'emphasis'})]
   120         return u'\n'.join(inputs)
   117         return u'\n'.join(inputs)
   121 
   118 
   122     def process_field_data(self, form, field):
   119     def process_field_data(self, form, field):
   123         passwd1 = super(PasswordInput, self).process_field_data(form, field)
   120         passwd1 = super(PasswordInput, self).process_field_data(form, field)
   124         fieldname = form.form_field_name(field)
   121         passwd2 = form._cw.form.get(field.input_name(form, '-confirm'))
   125         passwd2 = form._cw.form[fieldname+'-confirm']
       
   126         if passwd1 == passwd2:
   122         if passwd1 == passwd2:
   127             if passwd1 is None:
   123             if passwd1 is None:
   128                 return None
   124                 return None
   129             return passwd1.encode('utf-8')
   125             return passwd1.encode('utf-8')
   130         raise ProcessFormError(form._cw._("password and confirmation don't match"))
   126         raise ProcessFormError(form._cw._("password and confirmation don't match"))
   181         linecount = len(lines)
   177         linecount = len(lines)
   182         for line in lines:
   178         for line in lines:
   183             linecount += len(line) / 80
   179             linecount += len(line) / 80
   184         attrs.setdefault('cols', 80)
   180         attrs.setdefault('cols', 80)
   185         attrs.setdefault('rows', min(15, linecount + 2))
   181         attrs.setdefault('rows', min(15, linecount + 2))
   186         return tags.textarea(value, name=name, **attrs)
   182         return tags.textarea(value, name=field.input_name(form), **attrs)
   187 
   183 
   188 
   184 
   189 class FCKEditor(TextArea):
   185 class FCKEditor(TextArea):
   190     """FCKEditor enabled <textarea>"""
   186     """FCKEditor enabled <textarea>"""
   191     def __init__(self, *args, **kwargs):
   187     def __init__(self, *args, **kwargs):
   229                                            selected='selected', **oattrs))
   225                                            selected='selected', **oattrs))
   230             else:
   226             else:
   231                 options.append(tags.option(label, value=value, **oattrs))
   227                 options.append(tags.option(label, value=value, **oattrs))
   232         if optgroup_opened:
   228         if optgroup_opened:
   233             options.append(u'</optgroup>')
   229             options.append(u'</optgroup>')
   234         return tags.select(name=name, multiple=self._multiple,
   230         return tags.select(name=field.input_name(form), multiple=self._multiple,
   235                            options=options, **attrs)
   231                            options=options, **attrs)
   236 
   232 
   237 
   233 
   238 class CheckBox(Input):
   234 class CheckBox(Input):
   239     """<input type='checkbox'>, for field having a specific vocabulary. One
   235     """<input type='checkbox'>, for field having a specific vocabulary. One
   257             iattrs.update(oattrs)
   253             iattrs.update(oattrs)
   258             if i == 0 and domid is not None:
   254             if i == 0 and domid is not None:
   259                 iattrs.setdefault('id', domid)
   255                 iattrs.setdefault('id', domid)
   260             if value in curvalues:
   256             if value in curvalues:
   261                 iattrs['checked'] = u'checked'
   257                 iattrs['checked'] = u'checked'
   262             tag = tags.input(name=name, type=self.type, value=value, **iattrs)
   258             tag = tags.input(name=field.input_name(form), type=self.type,
       
   259                              value=value, **iattrs)
   263             options.append(tag + label)
   260             options.append(tag + label)
   264         return sep.join(options)
   261         return sep.join(options)
   265 
   262 
   266 
   263 
   267 class Radio(CheckBox):
   264 class Radio(CheckBox):
   349 
   346 
   350     def _render_calendar_popup(self, form, field):
   347     def _render_calendar_popup(self, form, field):
   351         value = form.form_field_value(field)
   348         value = form.form_field_value(field)
   352         if not value:
   349         if not value:
   353             value = date.today()
   350             value = date.today()
   354         inputid = form.context[field]['id']
   351         inputid = field.dom_id(form)
   355         helperid = '%shelper' % inputid
   352         helperid = '%shelper' % inputid
   356         year, month = value.year, value.month
   353         year, month = value.year, value.month
   357         return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
   354         return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
   358 <img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
   355 <img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
   359                 % (helperid, inputid, year, month,
   356                 % (helperid, inputid, year, month,