Validation error message

Hello,

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; }

The validation error should be:

‘Šifra’ je obavezan podatak

Is there an easy way to achieve this?

Thanks!

It’s not possible with Declarative Validation Attributes, you would need to use Fluent Validation for customization.

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");
        });
}

The way you customize Fluent Validation error messages is with a Custom Language Manager.

Hello,

I noticed that the Serbian translations in ServiceStack’s validation messages contain some typos.

FluentValidation provides a correct Serbian localization, which can be found here:
:link: FluentValidation Serbian Translation on GitHub

Would it be possible to update the SerbianLanguage class in ServiceStack using this version?

Thanks!

I’ve updated all Fluent Validation Languages in the latest v8.6.1 that’s now available in pre release packages.

1 Like