def extractData(self, fields): # XXX to review this cached = self.__extracted.get(fields) if cached is not None: return cached data = FieldsValues(self, fields) self.errors = 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."), self.prefix)) self.errors = errors return (data, errors)
def validate(self): convert_request_form_to_unicode(self.request.form) info = {'success': True} fieldname = self.request.form['prefix.field'] # We need to update the form first, since this is the common # place to configure more fields. self.context.update() try: lookup = IFormLookup(self.context).lookup( self.request.form['prefix.form']) except KeyError: info['success'] = False else: # Look for extractor, extract and validate value. for field in lookup.fields(): extractor = lookup.form.widgetFactory.extractor(field) if extractor is not None: if extractor.identifier == fieldname: value, error = extractor.extract() if error is None: error = field.validate(value, self.context) if error is not None: if not IError.providedBy(error): error = Error(title=error, identifier=fieldname) info['success'] = False info['errors'] = serialize_error(self, error) break else: info['success'] = False return self.json_response(info)
def validateData(self, fields, data): errors = Errors() for factory in self.dataValidators: validator = factory(self, fields) for error in validator.validate(data): if not IError.providedBy(error): error = Error(error, self.prefix) errors.append(error) return errors