public void AddValidation(Grid grid, DataViewBase dataView) { Action <string, Column> setValidator = delegate(string attributeName, Column col) { col.Validator = delegate(object value, object item) { Func <string, GridValidatorDelegate> indexer = dataView.GridValidationIndexer(); GridValidatorDelegate validationRule = indexer(attributeName); if (validationRule != null) { return(validationRule(value, item)); } else { ValidationResult result = new ValidationResult(); result.Valid = true; return(result); } }; }; if (dataView.GridValidationIndexer() != null) { foreach (Column col in grid.GetColumns()) { string fieldName = col.Field; setValidator(fieldName, col); } } }
public static GridValidatorDelegate ConvertToGridValidation(ValidationRuleDelegate ruleDelegate) { GridValidatorDelegate validationFunction = delegate(object value, object item) { ValidationRules rules = new ValidationRules(); rules = ruleDelegate(rules, item, null); ValidationResult result = new ValidationResult(); result.Valid = true; Dictionary validationRules = Dictionary.GetDictionary(rules); foreach (string key in validationRules.Keys) { // Find the ko validation rule if (ValidationApi.Rules.ContainsKey(key)) { ValidationRule targetRule = (ValidationRule)ValidationApi.Rules[key]; ValidationRule sourceRule = (ValidationRule)validationRules[key]; result.Valid = targetRule.Validator(value, sourceRule.Params == null ? targetRule.Params : sourceRule.Params); result.Message = String.IsNullOrEmpty(targetRule.Message) ? sourceRule.Message : targetRule.Message; } else if (key == "validation") { // Anon rule - can be either a single or array - we assume it's an array created by the ValidationRules class List <AnonymousRule> anonRules = (List <AnonymousRule>)validationRules[key]; foreach (AnonymousRule rule in anonRules) { result.Valid = rule.validator(value); result.Message = rule.message; if (!result.Valid) { break; } } } if (!result.Valid) { break; } } return(result); }; return(validationFunction); }