This is my model:
namespace MvcApplication2.Models
{
public class CreditCard
{
[CreditCard(ErrorMessageResourceType = typeof(Messages), Error
In my case, I was using ErrorMessage
as ""
in .Net Core 3.1 API.
[Required]
[RegularExpression("^[1-9]\\d*$", ErrorMessage ="")]
public int fieldName { get; set; }
I change to ErrorMessage ="Invalid Value Or some other message"
and the issue is solved.
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";
}
}
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 atrtribute 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.
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.
I had a simmilar issue with a custom ValidationAttribute.
In the IsValid method I was setting the ErrorMessage. The fix was to remove the assignation to the ErrorMessage propety...
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//code before ...
this.ErrorMessage = this.FormatErrorMessage(validationContext.DisplayName); //NOT GOOD...
ValidationResult validationResult = new ValidationResult(this.ErrorMessage, //And using the ErrorMessage Here...
new[] { validationContext.MemberName });
return validationResult;
}
I was writting Unit test and they were pasisng only if i was running/debugging one by one. But when I click "Run All", only the first one was passing? They are not Linked in any ways...
So yeah, just remove the remove the assignation to the ErrorMessage propety
I hope it will help someone!
Replacing
[Required(ErrorMessage = "")]
with
[Required()]
worked for me, there is no meaning of keeping ErrorMessage with a blank string either