Let's say I have a custom validation attribute:
public class CustomAttribute : ValidationAttribute
{
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var customErrorString = "You did something wrong!"; //How to pass this to the localized string defined in the resource file?
if(SomethingIsInvalid())
return new ValidationResult(FormatErrorMessage(validationContext.MemberName));
return ValidationResult.Success;
}
}
public class CustomAttributeAdapter : AttributeAdapterBase<CustomAttribute>
{
public CustomAttributeAdapter(CustomAttribute attribute, IStringLocalizer stringLocalizer)
: base(attribute, stringLocalizer)
{
}
public override void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
MergeAttribute(context.Attributes, "data-val", "true");
}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException(nameof(validationContext));
}
return GetErrorMessage(validationContext.ModelMetadata, validationContext.ModelMetadata.GetDisplayName());
}
}
How can I pass, let's say, a string from the data annotation to the validation tag? For example:
[Custom(ErrorMessage = "CustomValidationMessage")]
public string ValidateThis { get; set; }
CustomValidationMessage
is defined in a resource file and results to "This is invalid:"
Now my question is, how can I pass customErrorString
from the validation attribute to the localized string so it shows on the validation tag like this:
<span id="validation-span" asp-validation-for="@Model.ValidateThis" class="text-danger">This is invalid: You did something wrong!</span>
I hope my question is clear. If not, feel free to ask for more details.
EDIT: I got it to work:
public class CustomAttribute : ValidationAttribute
{
//Create property to hold our custom error string
public string CustomErrorString { get; set; }
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
//Set the custom error string property.
CustomErrorString = "You did something wrong!";
if(SomethingIsInvalid())
return new ValidationResult(FormatErrorMessage(validationContext.MemberName));
return ValidationResult.Success;
}
}
public class CustomAttributeAdapter : AttributeAdapterBase<CustomAttribute>
{
//Declare class variable to hold the attribute's custom error string.
private string _customErrorString = string.empty;
public CustomAttributeAdapter(CustomAttribute attribute, IStringLocalizer stringLocalizer)
: base(attribute, stringLocalizer)
{
//Set the adapter's custom error string according to the attribute's custom error string
if(!string.IsNullOrEmpty(attribute.CustomErrorString))
_customErrorString = attribute.CustomErrorString;
}
public override void AddValidation(ClientModelValidationContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
MergeAttribute(context.Attributes, "data-val", "true");
}
public override string GetErrorMessage(ModelValidationContextBase validationContext)
{
if (validationContext == null)
{
throw new ArgumentNullException(nameof(validationContext));
}
//Pass the custom error string instead of member name
return GetErrorMessage(validationContext.ModelMetadata, _customErrorString);
}
}
After you do all of this, you can then set your resource string like this:
[Custom(ErrorMessage = "CustomValidationMessage")]
public string ValidateThis { get; set; }
Where CustomValidationMessage
results in "This is invalid: {0}"
Normally, {0}
would result in the localized member name of the property. But because we passed the custom error string in the adapter, it will be set to the custom error message.
It might be a little dirty, but it gets the job done.