private void UpdateState() { Popup popup = (Popup)FindName("popup"); if (popup != null) { popup.IsOpen = false; } BusinessBase businessObject = Source as BusinessBase; if (businessObject != null) { // for some reason Linq does not work against BrokenRulesCollection... List <BrokenRule> allRules = new List <BrokenRule>(); foreach (var r in businessObject.BrokenRulesCollection) { if (r.Property == Property) { allRules.Add(r); } } var removeRules = (from r in BrokenRules where !allRules.Contains(r) select r).ToArray(); var addRules = (from r in allRules where !BrokenRules.Contains(r) select r).ToArray(); foreach (var rule in removeRules) { BrokenRules.Remove(rule); } foreach (var rule in addRules) { BrokenRules.Add(rule); } BrokenRule worst = (from r in BrokenRules orderby r.Severity select r).FirstOrDefault(); if (worst != null) { _worst = worst.Severity; } _isValid = BrokenRules.Count == 0; GoToState(true); } else { BrokenRules.Clear(); _isValid = true; GoToState(true); } }
/* http://www.codeproject.com/Tips/680381/Data-validation-framework-at-entity-level-using-Da */ public virtual bool Validate() { BrokenRules.Clear(); var validationResults = new List <ValidationResult>(); var validationContext = new ValidationContext(this, null, null); Validator.TryValidateObject(this, validationContext, validationResults, true); foreach (var error in validationResults) { BrokenRules.Add(new ValidationRule(error.MemberNames.First(), error.ErrorMessage)); } return(BrokenRules.Count == 0); }
/// <summary> /// Checks all validation rules against entity values. /// </summary> public void CheckAllRules() { Boolean foundBrokenRule = false; Boolean hasBrokenRules = BrokenValidationRulesCount != 0; BrokenRules.Clear(); foreach (String item in ValidationRulesManager.GetAllPropertyNamesWithRules()) { Object value = this.GetType().GetProperty(item).GetValue(this, null); if (CheckRulesForProperty(item, value, CalledByCheckAllRules.Yes)) { foundBrokenRule = true; } } if (foundBrokenRule || hasBrokenRules) { RaiseBrokenRuleFountNotification(foundBrokenRule); } }
/// <summary> /// Updates the state on control Property. /// </summary> protected virtual void UpdateState() { if (_loading) { return; } Popup popup = (Popup)FindChild(this, "popup"); if (popup != null) { popup.IsOpen = false; } if (Source == null || string.IsNullOrEmpty(PropertyName)) { BrokenRules.Clear(); RuleDescription = string.Empty; IsValid = true; CanWrite = false; CanRead = false; } else { var iarw = Source as Csla.Security.IAuthorizeReadWrite; if (iarw != null) { CanWrite = iarw.CanWriteProperty(PropertyName); CanRead = iarw.CanReadProperty(PropertyName); } BusinessBase businessObject = Source as BusinessBase; if (businessObject != null) { var allRules = (from r in businessObject.BrokenRulesCollection where r.Property == PropertyName select r).ToArray(); var removeRules = (from r in BrokenRules where !allRules.Contains(r) select r).ToArray(); var addRules = (from r in allRules where !BrokenRules.Contains(r) select r).ToArray(); foreach (var rule in removeRules) { BrokenRules.Remove(rule); } foreach (var rule in addRules) { BrokenRules.Add(rule); } IsValid = BrokenRules.Count == 0; if (!IsValid) { BrokenRule worst = (from r in BrokenRules orderby r.Severity select r).FirstOrDefault(); if (worst != null) { RuleSeverity = worst.Severity; RuleDescription = worst.Description; } else { RuleDescription = string.Empty; } } else { RuleDescription = string.Empty; } } else { BrokenRules.Clear(); RuleDescription = string.Empty; IsValid = true; } } GoToState(true); }