コード例 #1
0
        public void ConstructorWithNullStringArgumentCreatesEmptyStringErrorMessage() {
            // Act
            ModelError modelError = new ModelError((string)null);

            // Assert
            Assert.AreEqual(String.Empty, modelError.ErrorMessage);
        }
コード例 #2
0
        public void ConstructorWithStringArgument() {
            // Act
            ModelError modelError = new ModelError("some message");

            // Assert
            Assert.AreEqual("some message", modelError.ErrorMessage);
            Assert.IsNull(modelError.Exception);
        }
コード例 #3
0
        private static string GetErrorMessage(ModelError error, ModelState modelState)
        {
            if (!IsNullOrEmpty(error.ErrorMessage))
                return error.ErrorMessage;

            return modelState.Value == null
                       ? error.ErrorMessage
                       : Format(CultureInfo.CurrentCulture, ValueNotValidForProperty, modelState.Value.AttemptedValue);
        }
コード例 #4
0
ファイル: ValidationExtensions.cs プロジェクト: dpawatts/zeus
        private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
        {
            if (!string.IsNullOrEmpty(error.ErrorMessage))
                return error.ErrorMessage;

            if (modelState == null)
                return null;

            string value = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
            return string.Format(CultureInfo.CurrentCulture, "The value '{0}' is invalid.", value);
        }
コード例 #5
0
        public void ConstructorWithExceptionArgument() {
            // Arrange
            Exception ex = new Exception("some message");

            // Act
            ModelError modelError = new ModelError(ex);

            // Assert
            Assert.AreEqual(String.Empty, modelError.ErrorMessage);
            Assert.AreSame(ex, modelError.Exception);
        }
コード例 #6
0
        public void ConstructorWithExceptionAndStringArguments() {
            // Arrange
            Exception ex = new Exception("some message");

            // Act
            ModelError modelError = new ModelError(ex, "some other message");

            // Assert
            Assert.AreEqual("some other message", modelError.ErrorMessage);
            Assert.AreSame(ex, modelError.Exception);
        }
コード例 #7
0
        private static string GetErrorMessage(ModelError error, ModelState modelState)
        {
            if (!string.IsNullOrEmpty(error.ErrorMessage))
                return error.ErrorMessage;

            if (modelState.Value == null)
                return error.ErrorMessage;

            var args = new object[] {modelState.Value.AttemptedValue};
            return string.Format("ValueNotValidForProperty=El valor '{0}' es invalido ", args);
        }
コード例 #8
0
 private static string GetErrorMessage(ModelError error, ModelState modelState)
 {
     if (!string.IsNullOrEmpty(error.ErrorMessage))
     {
         return error.ErrorMessage;
     }
     if (modelState.Value == null)
     {
         return error.ErrorMessage;
     }
     object[] args = new object[] { modelState.Value.AttemptedValue };
     return string.Format("ValueNotValidForProperty=The value '{0}' is invalid", args);
 }
コード例 #9
0
        private static string GetErrorMessage(ModelError error, ModelState modelState)
        {
            if (!error.ErrorMessage.HasValue())
            {
                if (modelState.Value == null)
                {
                    return error.ErrorMessage;
                }

                return Exceptions.ValueNotValidForProperty.FormatWith(modelState.Value.AttemptedValue);
            }

            return error.ErrorMessage;
        }
コード例 #10
0
 private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
 {
     if (!string.IsNullOrEmpty(error.ErrorMessage))
         return error.ErrorMessage;
     else if (modelState == null)
     {
         return (string)null;
     }
     else
     {
         string str = modelState.Value != null ? modelState.Value.AttemptedValue : (string)null;
         return string.Format((IFormatProvider)CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), new object[1]
     {
       (object) str
     });
     }
 }
コード例 #11
0
        public static IHtmlString ValidationMessage(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, IDictionary <string, object> htmlAttributes)
        {
            htmlAttributes = htmlAttributes ?? new RouteValueDictionary();
            var    validationMessage = "";
            string fullHtmlFieldName = htmlAttributes["name"] == null ? modelMetadata.PropertyName : htmlAttributes["name"].ToString();

            if (!string.IsNullOrEmpty(htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix))
            {
                fullHtmlFieldName = htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix + "." + fullHtmlFieldName;
            }
            FormContext formContextForClientValidation = htmlHelper.ViewContext.FormContext;
            //if (htmlHelper.ViewContext.ClientValidationEnabled)
            //{
            //    formContextForClientValidation = htmlHelper.ViewContext.FormContext;
            //}
            //if (!htmlHelper.ViewData.ModelState.ContainsKey(fullHtmlFieldName) && (formContextForClientValidation == null))
            //{
            //    return null;
            //}
            ModelState           modelState = htmlHelper.ViewData.ModelState[fullHtmlFieldName];
            ModelErrorCollection errors     = (modelState == null) ? null : modelState.Errors;
            ModelError           error      = ((errors == null) || (errors.Count == 0)) ? null : errors[0];

            if ((error == null) && (formContextForClientValidation == null))
            {
                return(null);
            }
            TagBuilder builder = new TagBuilder("span");

            builder.MergeAttributes <string, object>(htmlAttributes);
            builder.AddCssClass((error != null) ? HtmlHelper.ValidationMessageCssClassName : HtmlHelper.ValidationMessageValidCssClassName);
            if (!string.IsNullOrEmpty(validationMessage))
            {
                builder.SetInnerText(validationMessage);
            }
            else if (error != null)
            {
                builder.SetInnerText(GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, error, modelState));
            }
            if (formContextForClientValidation != null)
            {
                bool replaceValidationMessageContents = String.IsNullOrEmpty(validationMessage);

                FieldValidationMetadata fieldMetadata = ApplyFieldValidationMetadata(htmlHelper, modelMetadata, fullHtmlFieldName);
                // rules will already have been written to the metadata object
                fieldMetadata.ReplaceValidationMessageContents = replaceValidationMessageContents; // only replace contents if no explicit message was specified

                if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
                {
                    builder.MergeAttribute("data-valmsg-for", fullHtmlFieldName);
                    builder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
                }
                else
                {
                    // client validation always requires an ID
                    builder.GenerateId(fullHtmlFieldName + "_validationMessage");
                    fieldMetadata.ValidationMessageId = builder.Attributes["id"];
                }
            }

            //if (formContext != null)
            //{
            //    bool replaceValidationMessageContents = String.IsNullOrEmpty(validationMessage);

            //    FieldValidationMetadata fieldMetadata = ApplyFieldValidationMetadata(htmlHelper, modelMetadata, modelName);
            //    // rules will already have been written to the metadata object
            //    fieldMetadata.ReplaceValidationMessageContents = replaceValidationMessageContents; // only replace contents if no explicit message was specified

            //    if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
            //    {
            //        builder.MergeAttribute("data-valmsg-for", modelName);
            //        builder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
            //    }
            //    else
            //    {
            //        // client validation always requires an ID
            //        builder.GenerateId(modelName + "_validationMessage");
            //        fieldMetadata.ValidationMessageId = builder.Attributes["id"];
            //    }
            //}
            return(new HtmlString(builder.ToString(TagRenderMode.Normal)));
        }
コード例 #12
0
ファイル: CommonModelBinder.cs プロジェクト: pworst/BoC
 private bool IsModelErrorAddedByMvc(ModelError modelError)
 {
     return modelError.Exception != null &&
         modelError.Exception is InvalidOperationException;
 }
コード例 #13
0
    private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
    {
      if (!String.IsNullOrEmpty(error.ErrorMessage))
      {
        return error.ErrorMessage;
      }
      if (modelState == null)
      {
        return null;
      }

      string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
      return String.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), attemptedValue);
    }
コード例 #14
0
        protected virtual string GetErrorMessage(ModelError error, ModelState modelState)
        {
            if (!error.ErrorMessage.HasValue())
            {
                if (modelState.Value == null)
                {
                    return error.ErrorMessage;
                }

                return TextResource.ValueNotValidForProperty.FormatWith(modelState.Value.AttemptedValue);
            }

            return error.ErrorMessage;
        }
コード例 #15
0
 private void AddErrors(ModelError result)
 {
     foreach (var error in result.ErrorMessage)
     {
         ModelState.AddModelError("", Convert.ToString(error));
     }
 }
コード例 #16
0
 private static string _GetUserErrorMessageOrDefault(ModelError error, ModelState modelState)
 {
     if (!string.IsNullOrEmpty(error.ErrorMessage))
     {
         return error.ErrorMessage;
     }
     if (modelState == null)
     {
         return null;
     }
     string str = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
     return string.Format(CultureInfo.CurrentCulture, "The value '{0}' is invalid.", new object[] { str });
 }
コード例 #17
0
ファイル: CommonModelBinder.cs プロジェクト: pworst/BoC
 private bool IsMvcModelBinderFormatException(ModelError modelError)
 {
     return modelError.Exception != null &&
            modelError.Exception.InnerException != null &&
            (
                modelError.Exception.InnerException is FormatException
                ||
                (modelError.Exception.InnerException.InnerException != null && modelError.Exception.InnerException.InnerException is FormatException)
            );
 }
コード例 #18
0
 private static string FormatModelStateErrorMessage(ModelError e)
 {
     return string.Format("{0}\r\n{1}", e.ErrorMessage,
         e.Exception != null ? e.Exception.ToString() : "");
 }
コード例 #19
0
        private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
        {
            if (!string.IsNullOrEmpty(error.ErrorMessage))
            {
                return(error.ErrorMessage);
            }
            if (modelState == null)
            {
                return(null);
            }
            string str = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;

            return(string.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), new object[] { str }));
        }
コード例 #20
0
 private bool IsModelErrorAddedByMvc(ModelError modelError)
 {
     return modelError.Exception != null &&
         modelError.Exception.GetType().Equals(typeof(InvalidOperationException));
 }
コード例 #21
0
 private bool IsMvcModelBinderFormatException(ModelError modelError)
 {
     return modelError.Exception != null &&
         modelError.Exception.InnerException != null &&
         modelError.Exception.InnerException.GetType().Equals(typeof(FormatException));
 }
コード例 #22
0
        private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
        {
            if (!string.IsNullOrEmpty(error.ErrorMessage))
                return error.ErrorMessage;
            if (modelState == null)
                return null;

            string str = modelState.Value != null ? modelState.Value.AttemptedValue : null;
            return string.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), new object[] { str });
        }
コード例 #23
0
		private static string ErrorToString(ModelError modelError) {
			if(modelError.Exception!=null) {
				return modelError.Exception.ToString();
			}
			return modelError.ErrorMessage;
		}