Esempio n. 1
0
        /// <summary>
        /// Validates the specified properties.
        /// </summary>
        /// <param name="propertyNames">The property names.</param>
        public void Validate(params string[] propertyNames)
        {
            var a = ValidationResults.Select(e => e.PropertyName).Distinct();

            if (Validator == null)
            {
                ValidationResults = ValidationResults.Except(propertyNames).ToList();
            }
            else
            {
                var validationSelector = propertyNames.Any()
                                        ? ValidatorOptions.ValidatorSelectors.MemberNameValidatorSelectorFactory(propertyNames)
                                        : ValidatorOptions.ValidatorSelectors.DefaultValidatorSelectorFactory();
                var validationContext = new ValidationContext(this, new PropertyChain(), validationSelector);
                var results           = Validator.Validate(validationContext).Errors;
                if (propertyNames.Length == 0)
                {
                    ValidationResults = results;
                }
                else
                {
                    ValidationResults = ValidationResults.Except(propertyNames).Concat(results).ToList();
                }
            }
            OnPropertyChanged(nameof(ValidationResults));
            var b = ValidationResults.Select(e => e.PropertyName).Distinct();

            foreach (var propertyName in a.Concat(b).Distinct())
            {
                OnErrorsChanged(propertyName);
            }
        }
		public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
		{
			Dictionary<Type, Validator> validators = new Dictionary<Type, Validator>();
			Int32 i = 0;
			ValidationResults results = new ValidationResults();
			IMethodReturn result = null;

			foreach (Type type in input.MethodBase.GetParameters().Select(p => p.ParameterType))
			{
				if (validators.ContainsKey(type) == false)
				{
					validators[type] = ValidationFactory.CreateValidator(type, this.Ruleset);
				}

				Validator validator = validators[type];
				validator.Validate(input.Arguments[i], results);

				++i;
			}

			if (results.IsValid == false)
			{
				result = input.CreateExceptionMethodReturn(new Exception(String.Join(Environment.NewLine, results.Select(r => r.Message).ToArray())));
			}
			else
			{
				result = getNext()(input, getNext);
			}

			return (result);
		}
        /// <summary>
        /// Determines whether the specified object is valid.
        /// </summary>
        /// <typeparam name="T">Generic Type</typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns><c>true</c> if the specified object is valid; otherwise, <c>false</c>.</returns>
        public bool IsValid <T>(T obj, out List <string> errorMessages)
        {
            errorMessages = new List <string>();
            Validator         cusValidator = new ObjectValidator();
            ValidationResults valResults   = cusValidator.Validate(obj);

            if (!valResults.IsValid)
            {
                errorMessages.AddRange(valResults.Select(objVal => objVal.Message));
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
 public IEnumerable <string> GetErrorMessages()
 {
     return(ValidationResults.Select(validationResult => validationResult.ErrorMessage).ToList());
 }
Esempio n. 5
0
        /// <summary>
        /// Valide l'état du model de façon synchrone
        /// </summary>
        void InternalUpdateValidationState()
        {
            IsValidating = true;

            try
            {
                bool?wasValid = null;

                // Stocke les propriétés qui étaient en erreur
                IList <string> invalidKeys = null;

                // Récupère les propriétés qui étaient en erreur
                if (_validationResults != null)
                {
                    invalidKeys = _validationResults.Select(vr => vr.Key).ToList();

                    if (!invalidKeys.Any())
                    {
                        invalidKeys = null;
                    }

                    wasValid = _validationResults.IsValid;
                }

                // Valide l'objet
                _validationResults = _validator.Validate(this);

                if (invalidKeys != null)
                {
                    // Parcourt les propriétés qui étaient en erreur auparavant
                    foreach (string key in invalidKeys)
                    {
                        // S'il y a eu un changement d'état, il faut en prévenir les bindings
                        if (!_validationResults.Any(vr => vr.Key == key))
                        {
                            OnPropertyChanged(key, false);
                        }
                    }
                }

                if (!_validationResults.IsValid)
                {
                    // Parcourt des propriétés qui sont en erreur
                    foreach (var result in _validationResults.Where(vr => vr.Target == this))
                    {
                        OnPropertyChanged(result.Key, false);
                    }
                }

                // Si l'objet change d'état de validité, on prévient les bindings
                if (!wasValid.HasValue || wasValid.Value != _validationResults.IsValid)
                {
                    OnIsValidChanged();
                }

                if (!(invalidKeys == null && _validationResults.IsValid))
                {
                    OnPropertyChanged(nameof(Error), false);
                    OnPropertyChanged(nameof(Errors), false);
                }
            }
            finally
            {
                IsValidating = false;
            }
        }
 /// <summary>
 /// Returns a string that represents the current object.
 /// </summary>
 /// <returns>A string that represents the current object.</returns>
 public override string ToString()
 {
     return(String.Join(Environment.NewLine, ValidationResults.Select(vr => vr.ErrorMessage)));
 }