def __call__(self): widgets = form.setUpWidgets( self.form_fields, 'form', self.context, self.request) results = [] data ={} if 'submit' in self.request: # get input data errors = form.getWidgetsData(widgets, 'form', data) invariant_errors = form.checkInvariants(self.form_fields, data) if errors: results.append('<h4>there were errors</h4>') if invariant_errors: results.append('<h4>there were invariant errors</h4>') for error in invariant_errors: results.append( "<p style='color:red'>%s</p>" % error ) for w in widgets: results.append("<p>%s</p>" % w()) error = w.error() if error: results.append("<p style='color:red'>%s</p>" % error) results = '\n'.join(results) return """<html><body> <form>%s <input type="submit" name="submit" value="submit"></form> <p>updated: %s</p> </body></html> """ % (results, str(data))
def __call__(self): widgets = form.setUpWidgets(self.form_fields, 'form', self.context, self.request) results = [] data = {} if 'submit' in self.request: # get input data errors = form.getWidgetsData(widgets, 'form', data) invariant_errors = form.checkInvariants(self.form_fields, data) if errors: results.append('<h4>there were errors</h4>') if invariant_errors: results.append('<h4>there were invariant errors</h4>') for error in invariant_errors: results.append("<p style='color:red'>%s</p>" % error) for w in widgets: results.append("<p>%s</p>" % w()) error = w.error() if error: results.append("<p style='color:red'>%s</p>" % error) results = '\n'.join(results) return """<html><body> <form>%s <input type="submit" name="submit" value="submit"></form> <p>updated: %s</p> </body></html> """ % (results, str(data))
def validate(self, action, data): """Validation that require context must be called here, invariants may be defined in the descriptor.""" errors = (form.getWidgetsData(self.widgets, self.prefix, data) + form.checkInvariants(self.form_fields, data)) if not errors and self.CustomValidation is not None: return list(self.CustomValidation(self.context, data)) return errors
def validate(self, action, data): """Validation that require context must be called here, invariants may be defined in the descriptor.""" errors = ( form.getWidgetsData(self.widgets, self.prefix, data) + form.checkInvariants(self.form_fields, data)) if not errors and self.CustomValidation is not None: return list(self.CustomValidation(self.context, data)) return errors
def validateUnique( self, action, data ): """ verification method for adding or editing against anything existing that conflicts with an existing row, as regards unique column constraints. this is somewhat costly as we basically reinvoke parsing form data twice, we need to extend zope.formlib to ignore existing values in the data dictionary to avoid this. its also costly because we're doing a query per unique column data we find, in order to unambigously identify the field thats problematic. but presumably those are very fast due to the unique constraint. TODO : assumes column == attribute """ errors = form.getWidgetsData( self.widgets, self.prefix, data ) + \ form.checkInvariants(self.form_fields, data) if errors: return errors domain_model = removeSecurityProxy( self.getDomainModel() ) # find unique columns in data model.. TODO do this statically mapper = orm.class_mapper( domain_model ) ucols = list( unique_columns( mapper ) ) errors = [] # query out any existing values with the same unique values, s = Session() # find data matching unique columns for key, col in ucols: if key in data: # on edit ignore new value if its the same as the previous value if isinstance( self.context, domain_model ) \ and data[key] == getattr( self.context, key, None): continue value = s.query( domain_model ).filter( col == data[key] ).count() if not value: continue widget = self.widgets[ key ] error = form.WidgetInputError( widget.name, widget.label, u"Duplicate Value for Unique Field" ) widget._error = error errors.append( error ) if errors: return tuple(errors)
def validateUnique(self, action, data): """ verification method for adding or editing against anything existing that conflicts with an existing row, as regards unique column constraints. this is somewhat costly as we basically reinvoke parsing form data twice, we need to extend zope.formlib to ignore existing values in the data dictionary to avoid this. its also costly because we're doing a query per unique column data we find, in order to unambigously identify the field thats problematic. but presumably those are very fast due to the unique constraint. TODO : assumes column == attribute """ errors = form.getWidgetsData(self.widgets, self.prefix, data) + \ form.checkInvariants(self.form_fields, data) if errors: return errors domain_model = removeSecurityProxy(self.getDomainModel()) # find unique columns in data model.. TODO do this statically mapper = orm.class_mapper(domain_model) ucols = list(unique_columns(mapper)) errors = [] # query out any existing values with the same unique values, s = bungeni.alchemist.Session() # find data matching unique columns for key, col in ucols: if key in data: # on edit ignore new value if its the same as the previous value if isinstance(self.context, domain_model) \ and data[key] == getattr(self.context, key, None): continue value = s.query(domain_model).filter(col == data[key]).count() if not value: continue widget = self.widgets[key] error = form.WidgetInputError( widget.name, widget.label, u"Duplicate Value for Unique Field") widget._error = error errors.append(error) if errors: return tuple(errors)
def validate_widgets(self, data, names=None): """Validate the named form widgets. :param names: Names of widgets to validate. If None, all widgets will be validated. """ if names is None: # Validate all widgets. widgets = self.widgets else: widgets = [] for input, widget in self.widgets.__iter_input_and_widget__(): if widget.context.__name__ in names: widgets.append((input, widget)) widgets = form.Widgets(widgets, len(self.prefix) + 1) for error in form.getWidgetsData(widgets, self.prefix, data): self.errors.append(error) for error in form.checkInvariants(self.form_fields, data): self.addError(error) return self.errors
def validate(self, action, data): return (form.getWidgetsData(self.widgets, self.prefix, data) + form.checkInvariants(self.form_fields, data))