I am using [ValidateNotEmpty] attribute in my DTOs and want to customize the error message by specifying a user-friendly property name and a fixed message format.
For example:
[ValidateMyNotEmpty(PropName = “Šifra”)]
public string Sifra { get; set; }
Ok, is it possible to use the default declarative validators and only change the default error message without modifying {PropertyName}?
I tried the following approach, but it doesn’t work:
using ServiceStack.Validation;
[assembly: HostingStartup(typeof(ConfigureValidation))]
namespace Synapse.Server;
public class ConfigureValidation : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureAppHost(appHost =>
{
var validation = appHost.Plugins.Find(x => x is ValidationFeature) as ValidationFeature;
validation?.ErrorCodeMessages.Add("NotEmpty", "{PropertyName} je obavezan podatak");
});
}