Changeset 7846 for main/waeup.kofa/trunk/src/waeup
- Timestamp:
- 12 Mar 2012, 13:06:45 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/widgets/phonewidget.py
r7842 r7846 249 249 250 250 from zc.sourcefactory.contextual import BasicContextualSourceFactory 251 from zope.formlib.textwidgets import TextWidget, renderElement 251 from zope.formlib.textwidgets import ( 252 TextWidget, renderElement, ConversionError) 253 from zope.formlib.widget import ValidationError 252 254 from zope.component import getUtility 253 255 from zope.interface import Invalid 254 from zope.formlib.interfaces import WidgetInputError, MissingInputError 256 from zope.formlib.interfaces import ( 257 WidgetInputError, MissingInputError, InputErrors,) 258 from zope.schema.interfaces import RequiredMissing 259 from waeup.kofa.interfaces import MessageFactory as _ 255 260 256 261 class IInternationalPhonePrefixes(Interface): … … 259 264 260 265 INT_PHONE_PREFIXES = { 261 'Germany': '49',262 'Nigeria': '234',263 'U.S.': '1',266 _('Germany'): '49', 267 _('Nigeria'): '234', 268 _('U.S.'): '1', 264 269 } 265 270 … … 273 278 for x,y in self._data]) 274 279 280 RE_NUMBERS = re.compile('^\d+$') 281 RE_NUMBERS_AND_HYPHENS = re.compile('^[\d\-]+$') 282 275 283 class PhoneWidget2(TextWidget): 276 284 277 285 subwidget_names = ('country', 'area', 'ext') 278 279 #_missing = '--'280 286 281 287 def _renderPrefixWidget(self, value): … … 340 346 """ 341 347 result = '-'.join( 342 [self.request.get('%s.%s' % (self.name, name) )348 [self.request.get('%s.%s' % (self.name, name), '') 343 349 for name in self.subwidget_names]) 344 350 return result 345 351 352 def _toFieldValue(self, input): 353 """Check value entered in form further. 354 355 Raises ConversionError if values entered contain non-numbers. 356 357 For the extension line we silently allow slashes as well. 358 """ 359 result = super(PhoneWidget2, self)._toFieldValue(input) 360 parts = input.split('-', 2) 361 if '' in parts and self.context.required: 362 raise ConversionError( 363 _("Empty phone field(s)."), MissingInputError( 364 self.name, self.label, None)) 365 if not RE_NUMBERS.match(parts[1]) or not RE_NUMBERS_AND_HYPHENS.match( 366 parts[2]): 367 raise ConversionError( 368 _("Phone numbers may contain numbers only."), 369 ValueError('non numbers in phone number')) 370 return result 371 372 def _getFormValue(self): 373 """Returns a value suitable for use in an HTML form. 374 375 Detects the status of the widget and selects either the input value 376 that came from the request, the value from the _data attribute or the 377 default value. 378 """ 379 try: 380 input_value = self._getCurrentValueHelper() 381 except InputErrors: 382 form_value = '-'.join( 383 [self.request.form.get('%s.%s' % (self.name, name), '') 384 for name in self.subwidget_names] 385 ) 386 if form_value == '--': 387 form_value = self._missing 388 else: 389 form_value = self._toFormValue(input_value) 390 return form_value 391 346 392 def hasInput(self): 393 """A phone widget has input if all three subfields have input. 394 """ 347 395 for name in self.subwidget_names: 348 396 if '%s.%s' % (self.name, name) not in self.request.form: 349 397 return False 350 398 return True 399
Note: See TracChangeset for help on using the changeset viewer.