web/formwidgets.py
changeset 3388 b8be8fc77c27
parent 3106 226da883d17a
parent 3387 a357d4147eee
child 3451 6b46d73823f5
equal deleted inserted replaced
3369:7b88d12b4ee2 3388:b8be8fc77c27
     9 
     9 
    10 from datetime import date
    10 from datetime import date
    11 from warnings import warn
    11 from warnings import warn
    12 
    12 
    13 from cubicweb.common import tags, uilib
    13 from cubicweb.common import tags, uilib
    14 from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE
    14 from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE, ProcessFormError
    15 
    15 
    16 
    16 
    17 class FieldWidget(object):
    17 class FieldWidget(object):
    18     """abstract widget class"""
    18     """abstract widget class"""
    19     # javascript / css files required by the widget
    19     # javascript / css files required by the widget
    61             attrs['id'] = form.context[field]['id']
    61             attrs['id'] = form.context[field]['id']
    62         if self.settabindex and not 'tabindex' in attrs:
    62         if self.settabindex and not 'tabindex' in attrs:
    63             attrs['tabindex'] = form.req.next_tabindex()
    63             attrs['tabindex'] = form.req.next_tabindex()
    64         return name, values, attrs
    64         return name, values, attrs
    65 
    65 
       
    66     def process_field_data(self, form, field):
       
    67         formkey = form.form_field_name(field)
       
    68         posted = form.req.form
       
    69         return posted.get(formkey)
    66 
    70 
    67 class Input(FieldWidget):
    71 class Input(FieldWidget):
    68     """abstract widget class for <input> tag based widgets"""
    72     """abstract widget class for <input> tag based widgets"""
    69     type = None
    73     type = None
    70 
    74 
   112                              **attrs),
   116                              **attrs),
   113                   '&#160;', tags.span(form.req._('confirm password'),
   117                   '&#160;', tags.span(form.req._('confirm password'),
   114                                       **{'class': 'emphasis'})]
   118                                       **{'class': 'emphasis'})]
   115         return u'\n'.join(inputs)
   119         return u'\n'.join(inputs)
   116 
   120 
       
   121     def process_field_data(self, form, field):
       
   122         passwd1 = super(PasswordInput, self).process_field_data(form, field)
       
   123         fieldname = form.form_field_name(field)
       
   124         passwd2 = form.req.form[fieldname+'-confirm']
       
   125         if passwd1 == passwd2:
       
   126             if passwd1 is None:
       
   127                 return None
       
   128             return passwd1.encode('utf-8')
       
   129         raise ProcessFormError(form.req._("password and confirmation don't match"))
   117 
   130 
   118 class PasswordSingleInput(Input):
   131 class PasswordSingleInput(Input):
   119     """<input type='password'> without a confirmation field"""
   132     """<input type='password'> without a confirmation field"""
   120     type = 'password'
   133     type = 'password'
   121 
   134 
       
   135     def process_field_data(self, form, field):
       
   136         value = super(PasswordSingleInput, self).process_field_data(form, field)
       
   137         if value is not None:
       
   138             return value.encode('utf-8')
       
   139         return value
   122 
   140 
   123 class FileInput(Input):
   141 class FileInput(Input):
   124     """<input type='file'>"""
   142     """<input type='file'>"""
   125     type = 'file'
   143     type = 'file'
   126 
   144