Example #1
0
        /// <summary>
        /// Validates the property.
        /// </summary>
        /// <param name="propertyName">Name of the property to validate.</param>
        /// <param name="value">The value of the property.</param>
        protected virtual void ValidateProperty(string propertyName, object value)
        {
            if (!ShouldValidateProperty(propertyName))
            {
                return;
            }

            var vc = new ValidationContext(this, null, null)
            {
                MemberName = propertyName,
            };
            var results = new List <ValidationResult>();
            var isValid = Validator.TryValidateProperty(value, vc, results);

            if (isValid)
            {
                ErrorsContainer.ClearErrors(propertyName);
            }
            else
            {
                ErrorsContainer.SetErrors(propertyName, results.Select(s => s.ErrorMessage));
            }

            OnValidationComplete();
        }
Example #2
0
        /// <summary>
        /// Validates this instance's properties.
        /// </summary>
        /// <param name="updateDisplay">if set to <c>true</c> the display is notified.</param>
        /// <returns><c>true</c> if the instance is valid.</returns>
        public virtual bool Validate(bool updateDisplay)
        {
            var vc      = new ValidationContext(this, null, null);
            var results = new List <ValidationResult>();
            var isValid = Validator.TryValidateObject(this, vc, results, true);

            if (!updateDisplay)
            {
                return(isValid);
            }

            if (isValid)
            {
                ErrorsContainer.ClearErrors();
            }
            else
            {
                ErrorsContainer.SetErrors(results);
            }

            OnValidationComplete();
            return(isValid);
        }