RegisterErrorMessage() публичный Метод

Registers the error message per PropertyInfo.
public RegisterErrorMessage ( PropertyInfo property, string message ) : void
property System.Reflection.PropertyInfo The property.
message string The message.
Результат void
Пример #1
0
        public void Validate(ErrorSummary errors)
        {
            if (Product == null)
            {
                errors.RegisterErrorMessage("Product", "The order must have a product.");
                return;
            }

            if (Quantity < Product.MinQuantity || Quantity > Product.MaxQuantity)
                errors.RegisterErrorMessage("Quantity", string.Format("The quantity {0} is invalid, it must be between {1} and {2}", Quantity, Product.MinQuantity, Product.MaxQuantity));
        }
		bool IValidationPerformer.PerformValidation(object objectInstance, IEnumerable<IValidator> validators, IEnumerable<IValidationContributor> contributors, RunWhen runWhen, ErrorSummary summaryToPopulate) {
			foreach (IValidator validator in validators) {
				if (!validator.IsValid(objectInstance)) {
					string name = validator.FriendlyName ?? validator.Name;
					summaryToPopulate.RegisterErrorMessage(name, validator.ErrorMessage);
				}
			}

			if(contributors != null)
				(this as IValidationPerformer).ExecuteContributors(objectInstance, contributors, summaryToPopulate, runWhen);

			bool isValid = !summaryToPopulate.HasError;
			return isValid;
		}
        public void Update([DataBind("details", Validate = true)] UserDetailsRequestDTO details)
        {
            if (Validator.IsValid(details))
            {
                var success = userService.UpdateDetails(details);

                if (!success)
                {
                    var errorSummary = new ErrorSummary();
                    errorSummary.RegisterErrorMessage(string.Empty, "There was a problem updating your details");
                    Flash["errors"] = errorSummary;
                }
            }
            else
                Flash["errors"] = Validator.GetErrorSummary(details);

            RedirectToAction("Show");
        }
Пример #4
0
        bool IValidationPerformer.PerformValidation(object objectInstance, IEnumerable <IValidator> validators, IEnumerable <IValidationContributor> contributors, RunWhen runWhen, ErrorSummary summaryToPopulate)
        {
            foreach (IValidator validator in validators)
            {
                if (!validator.IsValid(objectInstance))
                {
                    string name = validator.FriendlyName ?? validator.Name;
                    summaryToPopulate.RegisterErrorMessage(name, validator.ErrorMessage);
                }
            }

            if (contributors != null)
            {
                (this as IValidationPerformer).ExecuteContributors(objectInstance, contributors, summaryToPopulate, runWhen);
            }

            bool isValid = !summaryToPopulate.HasError;

            return(isValid);
        }
        public void Should_Fail_To_Send_Email_Because_Email_Is_Invalid()
        {
            // arrange
            const string errorMessage = "message";

            var email = new Email();
            var validatorRunner = new Mock<IValidatorRunner>();
            var errorSummary = new ErrorSummary();

            validatorRunner.Setup(v => v.IsValid(It.IsAny<Email>())).Returns(false);
            validatorRunner.Setup(v => v.GetErrorSummary(It.IsAny<Email>())).Returns(errorSummary);

            errorSummary.RegisterErrorMessage(errorMessage, errorMessage);

            // act
            var service = new EmailSenderService(validatorRunner.Object);

            TestDelegate act = () => service.Send(email);

            // assert
            Assert.That(act, Throws.InstanceOf<ValidationException>().With.Property("ValidationErrorMessages").EqualTo(new[] { errorMessage }));
        }
Пример #6
0
		protected bool CheckForValidationFailures(object instance, Type instanceType,
		                                          PropertyInfo prop, object value,
		                                          string name, string prefix,
		                                          ErrorSummary summary)
		{
			bool hasFailure = false;

			if (validator == null)
			{
				return false;
			}

			IValidator[] validators = validator.GetValidators(instanceType, prop);

			foreach(IValidator validatorItem in validators)
			{
				if (!validatorItem.IsValid(instance, value))
				{
					string propName = validatorItem.FriendlyName ?? validatorItem.Name;

					errors.Add(new DataBindError(prefix, prop.Name, validatorItem.ErrorMessage));

					summary.RegisterErrorMessage(propName, validatorItem.ErrorMessage);
					
					hasFailure = true;
				}
			}

			return hasFailure;
		}
Пример #7
0
		/// <summary>
		/// main validation logic happens here
		/// </summary>
		/// <param name="objectInstance">object instance to be validated</param>
		/// <param name="validators">the validators to run</param>
		/// <param name="summaryToPopulate"></param>
		protected virtual bool PerformValidation(object objectInstance, IEnumerable<IValidator> validators, ErrorSummary summaryToPopulate) 
		{
			bool isValid = true;

			foreach (IValidator validator in validators) {
				if (!validator.IsValid(objectInstance)) {
					string name = validator.FriendlyName ?? validator.Name;

					summaryToPopulate.RegisterErrorMessage(name, validator.ErrorMessage);

					isValid = false;
				}
			}

			SetErrorSummaryForInstance(objectInstance, summaryToPopulate);

			return isValid;
		}
Пример #8
0
 private void CreateErrorMessage(string message)
 {
     var errorSummary = new ErrorSummary();
     errorSummary.RegisterErrorMessage(string.Empty, message);
     Flash["error"] = errorSummary;
 }
		/// <summary>
		/// Registers the error message returned from an object validator
		/// as an error message for each individual indexed property.
		/// </summary>
		/// <param name="validator">The validator.</param>
		/// <param name="errors">The errors.</param>
		private void RegisterObjectValidatorErrorMessages(ObjectValidator validator, ErrorSummary errors)
		{
			ErrorSummary objectErrors = validator.ErrorSummary;
			foreach (string property in objectErrors.InvalidProperties)
			{
				foreach (string message in objectErrors.GetErrorsForProperty(property))
				{
					errors.RegisterErrorMessage(validator.FriendlyName + "." + property, message);
				}
			}
		}
		/// <summary>
		/// Appends the error to the <see cref="ErrorSummary">ErrorSummary</see>.
		/// </summary>
		/// <param name="validator">The validator.</param>
		/// <param name="errors">The errors.</param>
		protected virtual void AppendError(IValidator validator, ErrorSummary errors)
		{
			string name = validator.FriendlyName ?? validator.Name;

			ObjectValidator objectValidator = validator as ObjectValidator;
			if (objectValidator != null)
				RegisterObjectValidatorErrorMessages(objectValidator, errors);
			else 
				errors.RegisterErrorMessage(name, validator.ErrorMessage);
		}