Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        /* 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."
            });
        }
Ejemplo n.º 5
0
 /// <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()));
         }
     }
 }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
 public void AddBrokenRule(BusinessRule error)
 {
     BrokenRules.Add(error);
 }
Ejemplo n.º 8
0
 public CustomValidationException(string errorMessage, Exception ex)
 {
     BrokenRules.Add(string.Empty, errorMessage);
     _innerException = ex;
 }
Ejemplo n.º 9
0
 public CustomValidationException(string name, string description)
 {
     BrokenRules.Add(name, description);
 }
Ejemplo n.º 10
0
 public CustomValidationException(string errorMessage)
 {
     BrokenRules.Add(string.Empty, errorMessage);
 }
Ejemplo n.º 11
0
 protected void AddBrokenRule(BusinessRule businessRule)
 {
     BrokenRules.Add(businessRule);
 }