Either ErrorMessageString or ErrorMessageResourceName must be set, but not both error using CreditCardAttribute

This is my model:

namespace MvcApplication2.Models
{
    public class CreditCard
    {
        [CreditCard(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "CardNumberInvalid")]
        public string CardNumber { get; set; }
    }
}

This is my Messages.resx:

Name Value

CardNumberInvalid Check your card number is correct

And this is my view:

@model MvcApplication2.Models.CreditCard
@Html.TextBoxFor(m => m.CardNumber);

In MVC version 3 this works without error. In MVC 4 when I go to this page I get an excpetion saying “Either ErrorMessageString or ErrorMessageResourceName must be set, but not both”. This is only happening with the CreditCardAttribute. Other validation attributes such as RequiredAttribute work fine. I have only set the ErrorMessageResourceName. I have not set the ErrorMessageString, so do not understand what I have done wrong. Can anyone help please?

ErrorMessageResourceName and ErrorMessage cannot both be set, but I’m only setting one

This is the error I’m getting using MVC2: Either ErrorMessageString or ErrorMessageResourceName must be set, but not both. It is coming from the [Required] Validation DataAnnotation. Stack trace: In

Error: ‘Subscript indices must either be real positive integers or logicals’ when using Matlab .NET builder

I am using matlab NE builder to compile a dll file which I call from C#. The matlab code is confirmed to work in matlab, but when called from c# the following error occurs: An unhandled exception of

Subscript indices must either be real positive integers or logicals ERROR [duplicate]

This question already has an answer here: Subscript indices must either be real positive integers or logicals, generic solution 1 answer I have this strange error ‘Subscript indices must either

??? Subscript indices must either be real positive integers or logicals. Using findpeaks in MatLab

I’ve have been using findpeaks in MatLab to locate the maximum and minimum points of a waveform with no problem, but in the last 20 minutes or so the error: ??? Subscript indices must either be real p

Error parsing XML: unbound prefix – Element type “LinearLayout” must be followed by either attribute specifications

i am new to java and android. I need some help with my main.xml file. here is the error i have – error: Error parsing XML: unbound prefix – Element type LinearLayout must be followed by either attri

Error:Must have one-pixel frame that is either transparent or white in Android Studio

I have just imported a project from eclipse to Android studio. Almost for every second image , android studio is giving the following error. Error:Must have one-pixel frame that is either transparent

Matlab error ‘Subscript indices must either be real positive integers or logicals.’ [duplicate]

This question already has an answer here: Subscript indices must either be real positive integers or logicals, generic solution 1 answer I am new to matlab and matlab gives error ‘Subscript ind

java.sql.SQLException: error occurred during batching: batch must be either executed or cleared

I am getting java.sql.SQLException: error occurred during batching: batch must be either executed or cleared in the below code. I want to execute two queries. String sql_query=insert into m_status

XML error Element type “links” must be followed by either attribute specifications, “>” or “/>”

Hi guys I was wondering why I’m getting below error with below XML: 7: 88 Element type links must be followed by either attribute specifications, > or />. Is there a reason why this is ha

The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. I am getting this error in Entity Framework 4.4 when updating/migrat

Answers

It’s a known issue in .Net 4.5. Adding “ErrorMessage = null” named parameter should solve this.

Reference: http://connect.microsoft.com/VisualStudio/feedback/details/757298/emailaddress-attribute-is-unable-to-load-error-message-from-resource-mvc

I had this same problem on a property that I was localizing. I had defined the ErrorMessageResourceType and then the ErrorMessageResourceName but by mistake put the ErrorMessage attribe on as well which threw the exception

[NotEqualTo("User_Name", ErrorMessageResourceType = typeof(LROResources.Global), ErrorMessageResourceName = "UserPasswordCannotBeUsername", ErrorMessage = "The password cannot be the same as your Username.")]

So in my case by just removing ErrorMessage I fixed the problem.

I had this problem because I had implemented a RequiredAttributeAdapter, which was setting the ErrorMessageResourceName property automatically.

You can fix it by checking to see if the ErrorMessage property has been set before setting the ErrorMessageResourceName:

/// <summary>
/// Creates a new instance of the RequiredAttributeAdapter, used for switching the default required message
/// format
/// </summary>
public CustomMessageRequiredAttributeAdapter(
    ModelMetadata metadata,
    ControllerContext context,
    RequiredAttribute attribute
)
    : base(metadata, context, attribute)
{
    if (string.IsNullOrEmpty(attribute.ErrorMessage))
    {
        attribute.ErrorMessageResourceType = typeof (ValidationMessages);
        attribute.ErrorMessageResourceName = "PropertyValueRequired";
    }
}
[CreditCard(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "CardNumberInvalid"), , ErrorMessage = null]

will fix your problem

Since anyone who is using custom validation attributes and also wants to load error messages from resources for localization purposes, would face this problem i share my workaround here.
assume that you have a custom validation attribute like this one

[FileTypeMustBe("jpg")]
public HttpPostedFileBase MyFile {get; set;}

in your custom validation attribute add this code

public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentCulture,
          ErrorMessageString, name, ValidFileType);
    }

ValidFileType is name of a property which takes the input argument of custom validation attribute (jpg here),well now we can decorate our model property the way we like it to be

[FileTypeMustBe("jpg", ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "WrongFileTypeError")]
public HttpPostedFileBase MyFile {get; set;}

as you see there’s no need to add ErrorMessage=null anymore cause we took care of it in custom validation class. just do not forget if you had initialized any error message string in your custom validation , you have to remove it now and use FormatErrorMessage instead.