Beispiel #1
0
    def extractData(self, fields):
        # XXX to review this
        cached = self._extracted.get(fields)
        if cached is not None:
            return cached
        data = FieldsValues(self, fields)
        errors = Errors()
        self._extracted[fields] = (data, errors)

        for field in fields:
            if not field.available(self):
                continue

            # Widget extraction and validation
            extractor = self.widgetFactory.extractor(field)
            if extractor is not None:
                value, error = extractor.extract()
                if error is None:
                    error = field.validate(value, self)
                if error is not None:
                    if not IError.providedBy(error):
                        error = Error(error, extractor.identifier)
                    errors.append(error)
                data[field.identifier] = value

        # Generic form validation
        errors.extend(self.validateData(fields, data))
        if len(errors):
            # Add a form level error if not already present
            if self.prefix not in errors:
                errors.append(
                    Error(_(u"There were errors.", default=u"There were errors."),
                          self.prefix))
        self.errors = errors
        return (data, errors)
    def extractData(self, fields):
        data = FieldsValues(self, fields)
        errors = Errors()

        for field in fields:
            if not field.available(self):
                continue

            # Widget extraction and validation
            extractor = getWidgetExtractor(field, self, self.request)
            if extractor is not None:
                value, error = extractor.extract()
                if error is None:
                    error = field.validate(value, self.context)
                if error is not None:
                    if not interfaces.IError.providedBy(error):
                        if interfaces.IErrors.providedBy(error):
                            # this is an Errors, not implementing IError
                            # "Make it so, number one !"
                            error = Errors(
                                *error, identifier=extractor.identifier)
                        else:
                            error = Error(
                                error, identifier=extractor.identifier)
                    errors.append(error)
                data[field.identifier] = value

        # Generic form validation
        validation_errors = self.validateData(fields, data)
        errors.extend(validation_errors)
        self.errors = errors
        return (data, errors)
Beispiel #3
0
 def extract(self):
     value = self.request.form.get(self.identifier, NO_VALUE)
     if value is not NO_VALUE:
         try:
             value = int(value)
         except ValueError:
             return (None, u"Invalid internal input")
         collectedValues = []
         collectedErrors = Errors(identifier=self.identifier)
         for position in range(0, value):
             value_present = '%s.present.%d' % (
                 self.identifier, position) in self.request.form
             if not value_present:
                 # This value have been removed
                 continue
             field = self.valueField.clone(new_identifier=str(position))
             form = cloneFormData(self.form, prefix=self.identifier)
             data, errors = form.extractData(Fields(field))
             if errors:
                 collectedErrors.extend(errors)
             else:
                 collectedValues.append(data[field.identifier])
         if collectedErrors:
             return (None, collectedErrors)
         value = self.component.collectionType(collectedValues)
     return (value, None)