/// <summary> /// Checks rules for a property, validating the property against all shared and instance rules for the property. /// </summary> /// <param name="propertyName">Name of the property.</param> /// <param name="value">The value.</param> /// <param name="calledByCheckAllRules">The called by check all rules.</param> /// <returns><c>true</c> if this property is valid; otherwise, <c>false</c>.</returns> Boolean CheckRulesForProperty(String propertyName, Object value, CalledByCheckAllRules calledByCheckAllRules = CalledByCheckAllRules.No) { Boolean foundBrokenRule = false; Boolean removedBrokenRules = false; //CheckAllRules clears all BrokenRules before calling, so no reason to run this code. if (calledByCheckAllRules == CalledByCheckAllRules.No) { foreach (var item in (from r in BrokenRules where r.PropertyName == propertyName select r).ToList()) { BrokenRules.Remove(item); removedBrokenRules = true; } } foreach (ValidationRule vr in ValidationRulesManager.GetRulesForProperty(propertyName)) { if (!vr.IsValid(value)) { BrokenRules.Add(new BrokenRule(vr.PropertyName, vr.ErrorMessage)); foundBrokenRule = true; } } //if CheckAllRules called this method, there is no reason to send out change // notification for every property since these properties are scoped to the entity. // CheckAllRules will raise the notificaitons when completed. if (calledByCheckAllRules == CalledByCheckAllRules.No && (foundBrokenRule || removedBrokenRules)) { RaiseBrokenRuleFountNotification(foundBrokenRule); } return(foundBrokenRule); }
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); }
public LoginViewController() { BrokenRules.Add(new BrokenRule() { Check = () => !string.IsNullOrWhiteSpace(InputObject.Username), Balance = "The Username is required." }); var emailValidator = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); BrokenRules.Add(new BrokenRule() { Check = () => emailValidator.IsMatch(InputObject.Username), Balance = "The Username must be a valid email address." }); BrokenRules.Add(new BrokenRule() { Check = () => !(InputObject.Password == null && InputObject.Password.Equals("")), Balance = "The Password is required." }); }
/// <summary> /// Validate the rules list /// </summary> /// <param name="list">list of rules</param> private void ValidateRulesList(List <ValidateRuleBase> list) { foreach (ValidateRuleBase rule in list) { PropertyInfo info = _parentObject.GetType().GetProperty(rule.PropertyName); if (info != null) { object value = info.GetValue(_parentObject, null); if (rule.Validate(value)) { BrokenRules.Remove(rule); } else { BrokenRules.Add(rule); } } else { throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", rule.PropertyName, _parentObject.GetType().ToString())); } } }
/// <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); }
public void AddBrokenRule(BusinessRule error) { BrokenRules.Add(error); }
public CustomValidationException(string errorMessage, Exception ex) { BrokenRules.Add(string.Empty, errorMessage); _innerException = ex; }
public CustomValidationException(string name, string description) { BrokenRules.Add(name, description); }
public CustomValidationException(string errorMessage) { BrokenRules.Add(string.Empty, errorMessage); }
protected void AddBrokenRule(BusinessRule businessRule) { BrokenRules.Add(businessRule); }