Ignore:
Timestamp:
1 Jun 2011, 13:38:51 (14 years ago)
Author:
uli
Message:

Add own getWidgetsDate function to retrieve bindings of errors to form
fields.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/converters.py

    r6258 r6260  
    242242from zope.interface import Interface
    243243from zope.formlib import form
     244from zope.formlib.form import (
     245    _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix)
    244246from zope.formlib.interfaces import IInputWidget
    245247from zope.publisher.browser import TestRequest
    246248
     249def getWidgetsData(widgets, form_prefix, data):
     250    # from zope.formlib.form
     251    errors = []
     252    form_prefix = expandPrefix(form_prefix)
     253
     254    for input, widget in widgets.__iter_input_and_widget__():
     255        if input and IInputWidget.providedBy(widget):
     256            name = _widgetKey(widget, form_prefix)
     257
     258            if not widget.hasInput():
     259                continue
     260
     261            try:
     262                data[name] = widget.getInputValue()
     263            except ValidationError, error:
     264                # convert field ValidationError to WidgetInputError
     265                error = WidgetInputError(widget.name, widget.label, error)
     266                errors.append((name, error))
     267            except InputErrors, error:
     268                errors.append((name, error))
     269
     270    return errors
     271
     272
    247273class DefaultConverter(Converter):
    248274
     
    250276    grok.provides(ISchemaTypeConverter)
    251277
    252     def __init__(self, iface):
     278    def __init__(self, iface, form_fields=None):
    253279        self.iface = iface
     280        self.form_fields = form_fields
     281        if self.form_fields is None:
     282            self.form_fields = form.Fields(iface)
     283        return
    254284
    255285    def applyRowData(self, data_dict, context):
    256286        request = TestRequest(form={})
    257         form_fields = form.Fields(self.iface)
     287        #form_fields = form.Fields(self.iface)
     288        form_fields = self.form_fields
    258289        for key, val in data_dict.items():
    259290            request.form['form.%s' % key] = val
    260291        widgets = form.setUpWidgets(
    261292            form_fields, 'form', context, request)
    262         errors = form.getWidgetsData(widgets, 'form', data_dict)
     293        #errors = form.getWidgetsData(widgets, 'form', data_dict)
     294        errors = getWidgetsData(widgets, 'form', data_dict)
     295        if errors:
     296            err_message = []
     297            for key, error in errors:
     298                message = error[1].args[0]
     299                err_message.append((key, message))
    263300        invariant_errors = form.checkInvariants(form_fields, data_dict)
    264301        if not errors and not invariant_errors:
Note: See TracChangeset for help on using the changeset viewer.