def __init__(self, fields, **kw): if len(fields) < 2: raise ValueError(_("combination must combine two or more fields")) for ix, field in enumerate(fields): if not IField.providedBy(field): raise DoesNotImplement(IField) field.__name__ = "combination_%02d" % ix self.fields = tuple(fields) super(Combination, self).__init__(**kw)
def __init__(self, fields, use_default_for_not_selected=False, **kw): if len(fields) < 2: raise ValueError(_("union must combine two or more fields")) for ix, field in enumerate(fields): if not IField.providedBy(field): raise DoesNotImplement(IField) field.__name__ = "unioned_%02d" % ix self.fields = tuple(fields) self.use_default_for_not_selected = use_default_for_not_selected super(Union, self).__init__(**kw)
"""fields $Id: field.py 4634 2006-01-06 20:21:15Z fred $ """ from zope import interface, i18n, schema, component from zope.schema.interfaces import ( ValidationError, WrongType, IField, IVocabularyTokenized) from zope.interface.exceptions import DoesNotImplement import zope.catalog.interfaces import zope.index.text.queryparser import zope.index.text.parsetree from zc.form import interfaces from zc.form.i18n import _ _no_unioned_field_validates = _( "No unioned field validates ${value}.") _range_less_error = _("${minimum} must be less than ${maximum}.") _range_less_equal_error = _( "${minimum} must be less than or equal to ${maximum}.") _combination_wrong_size_error = _("The value has the wrong number of members") _combination_not_a_sequence_error = _("The value is not a sequence") _bad_query = _("Invalid query.") # Union field that accepts other fields... class MessageValidationError(ValidationError): "ValidationError that takes a message" def __init__(self, message, mapping=None): if mapping is not None:
class UnionWidget(BaseWidget): _field_index = None no_value_label = _('union_field_label-no_value', "Not specified") no_value_hint = _('union_field_hint-no_value', '') def loadValueFromRequest(self): field = self.context missing_value = field.missing_value value = self.request.form.get(self.name) try: value = int(value) except (TypeError, ValueError): value = missing_value else: if value >= len(field.fields): value = missing_value else: self._field_index = value # value should be an int index of the active field active = field.fields[value].bind(self.context) if zc.form.interfaces.IOptionField.providedBy(active): return active.getValue() widget = component.getMultiAdapter((active, self.request), IInputWidget) widget.required = widget.context.required = self.required widget.setPrefix(self.name) try: return widget.getInputValue() except WidgetInputError as e: # recast with our name and title self._error = WidgetInputError(self.context.__name__, self.label, e.errors) return missing_value template = namedtemplate.NamedTemplate('default') def render(self, value): # choices = ({selected, identifier, widget},) # widget may be None, name may be None. field = self.context missing_value = field.missing_value choices = [] field_index = self._field_index if field_index is not None: chosen_field = field.fields[self._field_index] elif value is not missing_value: chosen_field = field.validField(value) else: chosen_field = None for ix, inner_field in enumerate(field.fields): selected = inner_field is chosen_field inner = inner_field.bind(field.context) identifier = "%s-%02d" % (self.name, ix) if zc.form.interfaces.IOptionField.providedBy(inner): widget = CompositeOptionWidget(inner, self.request) else: widget = component.getMultiAdapter((inner, self.request), IInputWidget) if selected: widget.setRenderedValue(value) elif self._renderedValueSet(): if field.use_default_for_not_selected: widget.setRenderedValue(inner.default) else: widget.setRenderedValue(inner.missing_value) widget.setPrefix(self.name) choices.append({ 'selected': selected, 'identifier': identifier, 'widget': widget, 'value': six.text_type(ix) }) if not field.required: ix += 1 selected = chosen_field is None identifier = "%s-%02d" % (self.name, ix) widget = NotChosenWidget(self.no_value_label, self.no_value_hint) choices.append({ 'selected': selected, 'identifier': identifier, 'widget': widget, 'value': six.text_type(ix) }) return self.template(choices=choices)
provide a simpler API that derived classes are expected to implement. """ from xml.sax.saxutils import escape, quoteattr from zope.interface import implements from zope.schema.interfaces import ValidationError from zope.formlib.widget import InputWidget from zope.formlib.widget import BrowserWidget from zope.formlib.interfaces import IBrowserWidget from zope.formlib.interfaces import IWidget, IInputWidget, WidgetInputError from zc.form.i18n import _ _msg_missing_single_value_display = _( _("widget-missing-single-value-for-display"), "") _msg_missing_multiple_value_display = _( _("widget-missing-multiple-value-for-display"), "") _msg_missing_single_value_edit = _( _("widget-missing-single-value-for-edit"), "(no value)") _msg_missing_multiple_value_edit = _( _("widget-missing-multiple-value-for-edit"), "(no value)") class BaseWidget(BrowserWidget, InputWidget): # Note to previous users of widgetapi: # .translate -> ._translate; .__prefix -> ._prefix; NullValue -> # ._data_marker; .__initial_value and .__calculated_value -> replaced # with ._data (because zope.formlib.utility.setUpWidget behavior changed # for the better)
provide a simpler API that derived classes are expected to implement. """ from xml.sax.saxutils import quoteattr from zope.interface import implementer from zope.schema.interfaces import ValidationError from zope.formlib.widget import InputWidget from zope.formlib.widget import BrowserWidget from zope.formlib.interfaces import IBrowserWidget from zope.formlib.interfaces import IInputWidget, WidgetInputError from zc.form.i18n import _ _msg_missing_single_value_display = _( _("widget-missing-single-value-for-display"), "") _msg_missing_multiple_value_display = _( _("widget-missing-multiple-value-for-display"), "") _msg_missing_single_value_edit = _(_("widget-missing-single-value-for-edit"), "(no value)") _msg_missing_multiple_value_edit = _( _("widget-missing-multiple-value-for-edit"), "(no value)") @implementer(IBrowserWidget, IInputWidget) class BaseWidget(BrowserWidget, InputWidget): # Note to previous users of widgetapi: # .translate -> ._translate; .__prefix -> ._prefix; NullValue -> # ._data_marker; .__initial_value and .__calculated_value -> replaced # with ._data (because zope.formlib.utility.setUpWidget behavior changed
$Id: field.py 4634 2006-01-06 20:21:15Z fred $ """ from zope import interface, i18n, schema, component from zope.interface.exceptions import DoesNotImplement from zope.schema.interfaces import IField from zope.schema.interfaces import ValidationError from zope.schema.interfaces import WrongType import zope.catalog.interfaces import zope.index.text.parsetree import zope.index.text.queryparser from zc.form import interfaces from zc.form.i18n import _ _no_unioned_field_validates = _( "No unioned field validates ${value}.") _range_less_error = _("${minimum} must be less than ${maximum}.") _range_less_equal_error = _( "${minimum} must be less than or equal to ${maximum}.") _combination_wrong_size_error = _("The value has the wrong number of members") _combination_not_a_sequence_error = _("The value is not a sequence") _bad_query = _("Invalid query.") # Union field that accepts other fields... class MessageValidationError(ValidationError): """ValidationError that takes a message""" def __init__(self, message, mapping=None):