public void ConstructorWithNullStringArgumentCreatesEmptyStringErrorMessage() {
            // Act
            ModelError modelError = new ModelError((string)null);

            // Assert
            Assert.AreEqual(String.Empty, modelError.ErrorMessage);
        }
        public void ConstructorWithStringArgument() {
            // Act
            ModelError modelError = new ModelError("some message");

            // Assert
            Assert.AreEqual("some message", modelError.ErrorMessage);
            Assert.IsNull(modelError.Exception);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
 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);
 }
        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;
        }
 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
     });
     }
 }
        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)));
        }
Exemple #12
0
 private bool IsModelErrorAddedByMvc(ModelError modelError)
 {
     return modelError.Exception != null &&
         modelError.Exception is InvalidOperationException;
 }
    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);
    }
        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;
        }
 private void AddErrors(ModelError result)
 {
     foreach (var error in result.ErrorMessage)
     {
         ModelState.AddModelError("", Convert.ToString(error));
     }
 }
 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 });
 }
Exemple #17
0
 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)
            );
 }
 private static string FormatModelStateErrorMessage(ModelError e)
 {
     return string.Format("{0}\r\n{1}", e.ErrorMessage,
         e.Exception != null ? e.Exception.ToString() : "");
 }
        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 }));
        }
 private bool IsModelErrorAddedByMvc(ModelError modelError)
 {
     return modelError.Exception != null &&
         modelError.Exception.GetType().Equals(typeof(InvalidOperationException));
 }
 private bool IsMvcModelBinderFormatException(ModelError modelError)
 {
     return modelError.Exception != null &&
         modelError.Exception.InnerException != null &&
         modelError.Exception.InnerException.GetType().Equals(typeof(FormatException));
 }
        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 });
        }
		private static string ErrorToString(ModelError modelError) {
			if(modelError.Exception!=null) {
				return modelError.Exception.ToString();
			}
			return modelError.ErrorMessage;
		}