Ignore:
Timestamp:
12 Mar 2012, 13:06:45 (13 years ago)
Author:
uli
Message:

Error handling with new phonewidget works better now. Still a sketch
and test errors to fix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/phonewidget.py

    r7842 r7846  
    249249
    250250from zc.sourcefactory.contextual import BasicContextualSourceFactory
    251 from zope.formlib.textwidgets import TextWidget, renderElement
     251from zope.formlib.textwidgets import (
     252    TextWidget, renderElement, ConversionError)
     253from zope.formlib.widget import ValidationError
    252254from zope.component import getUtility
    253255from zope.interface import Invalid
    254 from zope.formlib.interfaces import WidgetInputError, MissingInputError
     256from zope.formlib.interfaces import (
     257    WidgetInputError, MissingInputError, InputErrors,)
     258from zope.schema.interfaces import RequiredMissing
     259from waeup.kofa.interfaces import MessageFactory as _
    255260
    256261class IInternationalPhonePrefixes(Interface):
     
    259264
    260265INT_PHONE_PREFIXES = {
    261         'Germany': '49',
    262         'Nigeria': '234',
    263         'U.S.': '1',
     266        _('Germany'): '49',
     267        _('Nigeria'): '234',
     268        _('U.S.'): '1',
    264269        }
    265270
     
    273278                       for x,y in self._data])
    274279
     280RE_NUMBERS = re.compile('^\d+$')
     281RE_NUMBERS_AND_HYPHENS = re.compile('^[\d\-]+$')
     282
    275283class PhoneWidget2(TextWidget):
    276284
    277285    subwidget_names = ('country', 'area', 'ext')
    278 
    279     #_missing = '--'
    280286
    281287    def _renderPrefixWidget(self, value):
     
    340346        """
    341347        result = '-'.join(
    342             [self.request.get('%s.%s' % (self.name, name))
     348            [self.request.get('%s.%s' % (self.name, name), '')
    343349             for name in self.subwidget_names])
    344350        return result
    345351
     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
    346392    def hasInput(self):
     393        """A phone widget has input if all three subfields have input.
     394        """
    347395        for name in self.subwidget_names:
    348396            if '%s.%s' % (self.name, name) not in self.request.form:
    349397                return False
    350398        return True
     399
Note: See TracChangeset for help on using the changeset viewer.