def clear_errors_and_warnings(ctx, key): """Clears field errors and warnings for a given key.""" errorlist = iformal.IFormErrors(ctx, None).getFormErrors() for e in errorlist: if (isinstance(e, validation.FieldError)) or (isinstance(e, FieldWarning)): if e.fieldName == key: errorlist.remove(e)
def _filter_errors(self, filterlist, errorlist=None): def _check_filters(e): for f in filterlist: if not f(e): return False return True if errorlist is None: errorlist = iformal.IFormErrors(self.ctx, None).getFormErrors() res = [e for e in errorlist if _check_filters(e)] return res
def render_field(self, ctx, data): # The field we're rendering field = self.field # Get stuff from the context formData = iformal.IFormData(ctx) formErrors = iformal.IFormErrors(ctx, None) # Find any error if formErrors is None: error = None else: error = formErrors.getFieldError(field.key) # Build the error message if error is None: message = '' else: message = T.div(class_='message')[error.message] # Create the widget (it's created in __init__ as a hack) widget = self.widget # Build the list of CSS classes classes = [ 'field', field.type.__class__.__name__.lower(), widget.__class__.__name__.lower(), ] if field.type.required: classes.append('required') if field.cssClass: classes.append(field.cssClass) if error: classes.append('error') # Create the widget and decide the method that should be called if field.type.immutable: render = widget.renderImmutable else: render = widget.render # Fill the slots tag = ctx.tag tag.fillSlots('id', util.render_cssid(field.key)) tag.fillSlots('fieldId', [util.render_cssid(field.key), '-field']) tag.fillSlots('class', ' '.join(classes)) tag.fillSlots('label', field.label) tag.fillSlots('inputs', render(ctx, field.key, formData, formErrors)) tag.fillSlots('message', message) tag.fillSlots('description', T.div(class_='description')[field.description or '']) return ctx.tag
def _renderErrors(self, ctx, data): errors = iformal.IFormErrors(ctx, None) if errors is not None: errors = errors.getFormErrors() if not errors: return '' errorList = T.ul() for error in errors: if isinstance(error, validation.FormError): errorList[ T.li[ error.message ] ] for error in errors: if isinstance(error, validation.FieldError): item = self.original.getItemByName(error.fieldName) errorList[ T.li[ T.strong[ item.label, ' : ' ], error.message ] ] return T.div(class_='errors')[ T.p['Please correct the following errors:'], errorList ]
def finalize_validation(self): """Finalize validation. Check whether form has errors, and if so, raise a validation.FormError to make Formal take notice. The text for this FormError is unfortunately fixed. May be called multiple times to "bail out early". """ # If the form has errors, errordata is remembered as form data in # renderForm (form.py). # Errordata is created in form process function (form.py) and is an # independent dictionary containing errors and current data. # Errordata is always created even though there are no errors. Thus # there is no need to check whether the errordata is available or not. errors = iformal.IFormErrors(self.ctx, None) errorData = errors.data formData = self.form.data # XXX: TBD - form data does not contain erranuous keys and data. # Canonicalization inserts the keys and therefore they exists in form data. # This, however is similar to formal "common" behaviour. #for key in errorData.keys(): # errorData[key] = [formData.get(key)] # Check if there are any errors in the form. Raise exception if an error is found. if errors is not None: errwarnlist = errors.getFormErrors() realerrs = self._filter_errors([lambda e: isinstance(e, validation.FormsError), lambda e: not isinstance(e, FormsWarning)], errorlist=errwarnlist) if len(realerrs) > 0: # Remove duplicate errors for the same fields to avoid issue #842 self._filter_dups(errwarnlist) raise validation.FormError('Validation of entered data failed') else: pass
def _processForm( form, ctx, name ): # Remember the form ctx.remember(form, iformal.IForm) # Create a keyed tag that will render the form when flattened. tag = T.invisible(key=name)[inevow.IRenderer(form)] # Create a new context, referencing the above tag, so that we don't # pollute the current context with anything the form needs during # rendering. ctx = context.WovenContext(parent=ctx, tag=tag) # Find errors for *this* form and remember things on the context errors = iformal.IFormErrors(ctx, None) if errors is not None and errors.formName == name: ctx.remember(errors.data, iformal.IFormData) else: ctx.remember(None, iformal.IFormErrors) ctx.remember(form.data or {}, iformal.IFormData) return ctx
def _cbFormProcessingFailed(self, failure, ctx): e = failure.value failure.trap(validation.FormError, validation.FieldError) errors = iformal.IFormErrors(ctx) errors.add(failure.value) return errors
def add_error(self, key, msg): errors = iformal.IFormErrors(self.ctx, None) errors.add(FieldGlobalError(msg, self._combine(key)))
def add_global_error(self, msg): errors = iformal.IFormErrors(self.ctx, None) errors.add(GlobalError(msg))
def add_warning(self, key, msg): errors = iformal.IFormErrors(self.ctx, None) errors.add(FieldWarning(msg, self._combine(key)))