Esempio n. 1
0
        //--------------------------------------------------------------
        #region Creation & Cleanup
        //--------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatableObservableObject"/> class.
        /// </summary>
        public ValidatableObservableObject()
        {
            ErrorsContainer = new ErrorsContainer(propertyName =>
            {
                OnErrorsChanged(new DataErrorsChangedEventArgs(propertyName));
                OnPropertyChanged(new PropertyChangedEventArgs(HasErrorsPropertyName));
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Validates all properties using data annotation attributes.
        /// </summary>
        /// <returns>
        /// <see langword="true"/> if all properties are valid; otherwise, <see langword="false"/>
        /// if it has errors.
        /// </returns>
        /// <remarks>
        /// This method does not check for entity-level errors. It only validates properties using
        /// data annotation attributes.
        /// </remarks>
        public bool ValidateProperties()
        {
            // Get all the properties decorated with the ValidationAttribute.
            var propertiesToValidate = GetType().GetRuntimeProperties()
                                       .Where(c => c.GetCustomAttributes(typeof(ValidationAttribute)).Any());

            bool isValid = true;
            var  errors  = new List <string>();

            foreach (var propertyInfo in propertiesToValidate)
            {
                errors.Clear();
                isValid &= TryValidateProperty(propertyInfo, errors);
                ErrorsContainer.SetErrors(propertyInfo.Name, errors);
            }

            return(isValid);
        }
Esempio n. 3
0
 public Dictionary <string, List <string> > GetAllErrors()
 {
     return(ErrorsContainer.GetAllErrors());
 }
Esempio n. 4
0
        //--------------------------------------------------------------
        #region Methods
        //--------------------------------------------------------------

        /// <summary>
        /// Gets the validation errors for a specified property or for the entire object.
        /// </summary>
        /// <param name="propertyName">
        /// The name of the property to retrieve validation errors for; or <see langword="null"/> or
        /// <see cref="string.Empty"/> to retrieve entity-level errors.
        /// </param>
        /// <returns>The validation errors for the property or object.</returns>
        public IEnumerable GetErrors(string propertyName)
        {
            return(ErrorsContainer.GetErrors(propertyName));
        }