def applyWidgetsChanges(view, schema, target=None, names=None): """Updates an object with values from a view's widgets. `view` contained the widgets that perform the update. By default, the widgets will update the view's context. `target` can be specified as an alternative object to update. `schema` contrains the values provided by the widgets. `names` can be specified to update a subset of the schema constrained values. """ errors = [] changed = False if target is None: target = view.context for name, field in _fieldlist(names, schema): widget = getattr(view, name + '_widget') if IInputWidget.providedBy(widget) and widget.hasInput(): try: changed = widget.applyChanges(target) or changed except InputErrors, v: errors.append(v)
def _widgetHasStickyValue(widget): """Returns ``True`` if the widget has a sticky value. A sticky value is input from the user that should not be overridden by an object's current field value. E.g. a user may enter an invalid postal code, submit the form, and receive a validation error - the postal code should be treated as 'sticky' until the user successfully updates the object. """ return IInputWidget.providedBy(widget) and widget.hasInput()
def getWidgetsData(widgets, form_prefix, data): errors = [] form_prefix = expandPrefix(form_prefix) for input, widget in widgets.__iter_input_and_widget__(): if input and IInputWidget.providedBy(widget): name = _widgetKey(widget, form_prefix) if not widget.hasInput(): continue try: data[name] = widget.getInputValue() except ValidationError, error: # convert field ValidationError to WidgetInputError error = WidgetInputError(widget.name, widget.label, error) errors.append(error) except InputErrors, error: errors.append(error)
def getWidgetsData(view, schema, names=None): """Returns user entered data for a set of `schema` fields. The return value is a map of field names to data values. `view` is the view containing the widgets. `schema` is the schema that defines the widget fields. An optional `names` argument can be provided to specify an alternate list of field values to return. If `names` is not specified, or is ``None``, `getWidgetsData` will attempt to return values for all of the fields in the schema. A requested field value may be omitted from the result for one of two reasons: - The field is read only, in which case its widget will not have user input. - The field is editable and not required but its widget does not contain user input. If a field is required and its widget does not have input, `getWidgetsData` raises an error. A widget may raise a validation error if it cannot return a value that satisfies its field's contraints. Errors, if any, are collected for all fields and reraised as a single `WidgetsError`. """ result = {} errors = [] for name, field in _fieldlist(names, schema): widget = getattr(view, name + '_widget') if IInputWidget.providedBy(widget): if widget.hasInput(): try: result[name] = widget.getInputValue() except InputErrors, error: errors.append(error) elif field.required: errors.append(MissingInputError( name, widget.label, 'the field is required'))
def testCaptchaWidgetInterface(self): self.assertEqual(IInputWidget.implementedBy(CaptchaWidget), True)