Пример #1
0
        public static IEnumerable<Error> ConvertValidationResultToErrorList(ModelValidationResult result)
        {
            if (result == null)
                return null;

            return
                from error in result.Errors
                from memberName in error.MemberNames
                select new Error(false, error.GetMessage(memberName), memberName);
        }
Пример #2
0
        public void Should_return_invalid_if_any_errors_exist()
        {
            // Given
            var subject = new ModelValidationResult(new[] { new ModelValidationError("blah", "blah") });

            // When
            var result = subject.IsValid;

            // Then
            result.ShouldBeFalse();
        }
Пример #3
0
        public void Should_return_valid_when_no_errors_exist()
        {
            // Given
            var subject = new ModelValidationResult((IEnumerable<ModelValidationError>)null);

            // When
            var result = subject.IsValid;

            // Then
            result.ShouldBeTrue();
        }
Пример #4
0
        public static List<string> GetValidationErrors(ModelValidationResult result)
        {
            List<string> errors = new List<string>();

            foreach(var e in result.Errors)
            {
                foreach(var message in e.Value)
                {
                    errors.Add (message.ErrorMessage);
                }
            }

            return errors;
        }
Пример #5
0
        public static ActionResult AsErrorOnValidation(ModelValidationResult validationResult)
        {
            var result = new ActionResult(false);

            foreach (var error in validationResult.Errors)
            {
                foreach (var member in error.MemberNames)
                {
                    result.WithError(error.GetMessage(member));
                }
            }

            return result;
        }
Пример #6
0
        public static Response AsValidationError(this IResponseFormatter response, ModelValidationResult result)
        {
            var model = new { Errors = new List<string>() };

            foreach (var error in result.Errors)
            {
                foreach (var member in error.MemberNames)
                {
                    model.Errors.Add(error.GetMessage(member));
                }
            }

            return response.AsJson(model)
                           .WithStatusCode(400);
        }
Пример #7
0
 public void AddValidationErrors(ModelValidationResult result)
 {
     Model.Errors.AddRange(Helpers.ErrorHelper.ConvertValidationResultToErrorList(result));
 }