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 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 as v: errors.append(v) if errors: raise WidgetsError(errors) return changed
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 ensure_required_fields_have_input(widgets, data): errors = [] for widget in widgets: if not IInputWidget.providedBy(widget): continue if not widget.context.required or widget.hasInput(): continue name = widget.context.__name__ error = zope.formlib.interfaces.WidgetInputError( name, widget.label, zope.schema.interfaces.RequiredMissing(name)) widget._error = error errors.append(error) return errors
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 as error: errors.append(error) elif field.required: errors.append(MissingInputError( name, widget.label, 'the field is required')) if errors: raise WidgetsError(errors, widgetsData=result) return result
def showOptionalMarker(self, field_name): """Should the (Optional) marker be shown?""" widget = self.widgets[field_name] # Do not show the (Optional) marker for display (i.e. read-only) # widgets. if not IInputWidget.providedBy(widget): return False # Do not show for readonly fields. context = getattr(widget, "context", None) if getattr(context, "readonly", None): return False # Do not show the marker for required widgets or always submitted # widgets. Everything else gets the marker. return not (widget.required or IAlwaysSubmittedWidget.providedBy(widget))
def showOptionalMarker(self, field_name): """Should the (Optional) marker be shown?""" widget = self.widgets[field_name] # Do not show the (Optional) marker for display (i.e. read-only) # widgets. if not IInputWidget.providedBy(widget): return False # Do not show for readonly fields. context = getattr(widget, 'context', None) if getattr(context, 'readonly', None): return False # Do not show the marker for required widgets or always submitted # widgets. Everything else gets the marker. return not (widget.required or IAlwaysSubmittedWidget.providedBy(widget))
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 results(self, name): if not (name+'.search' in self.request): return None schema = self.context.schema setUpWidgets(self, schema, IInputWidget, prefix=name+'.field') # XXX inline the original getWidgetsData call in # zope.app.form.utility to lift the dependency on zope.app.form. data = {} errors = [] for name, field in getFieldsInOrder(schema): widget = getattr(self, name + '_widget') if IInputWidget.providedBy(widget): if widget.hasInput(): try: data[name] = widget.getInputValue() except InputErrors, error: errors.append(error) elif field.required: errors.append(MissingInputError( name, widget.label, 'the field is required'))