Artur Kedzior
Artur Kedzior

Reputation: 4263

Using custom validators with Fluent Validation on ASP.NET Core 1.1

I'm unable to get custom validators to work on the client side. In the ASP.NET MVC5 I used to use Simple Injector to register validatiors:

var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
container.Register<IValidatorFactory, ApplicationValidatorFactory>(Lifestyle.Singleton);
container.Register(typeof(IValidator<>), assemblies);
container.RegisterConditional(typeof(IValidator<>),
    typeof(ValidateNothingDecorator<>), 
    Lifestyle.Singleton, 
    context => !context.Handled);

and register all custom validators:

// Register Simple Injector validation factory in FV
FluentValidationModelValidatorProvider.Configure(provider =>
{
    provider.ValidatorFactory = new ApplicationValidatorFactory(container);
    provider.AddImplicitRequiredValidator = false;
    provider.Add(typeof(UniqueEmailValidator),
        (m, c, d, v) => new UniqueEmailPropertyValidator(m, c, d, v));
    provider.Add(typeof(UniqueUsernameValidator),
        (m, c, d, v) => new UniqueUsernamePropertyValidator(m, c, d, v));
    provider.Add(typeof(StringNoSpacesValidator),
        (m, c, d, v) => new StringNoSpacesPropertyValidator(m, c, d, v));
    provider.Add(typeof(PasswordStrengthValidator),
        (m, c, d, v) => new PasswordStrengthPropertyValidator(m, c, d, v));
});

Add these methods on the JS side:

jQuery.validator.addMethod("nospaces", function(value, element)   {
    return value.indexOf(" ") < 0 && value != ""; 
    }, language.username_has_spaces); 

jQuery.validator.unobtrusive.adapters.add("nospaces", function (options) {
    options.rules["nospaces"] = true;
    if (options.message) {
        options.messages["nospaces"] = options.message;
    }
});

jQuery.validator.unobtrusive.adapters.add("passwordmeter", function (options) {
    options.rules["passwordmeter"] = true;
    if (options.message) {
        options.messages["passwordmeter"] = options.message;
    }
});

and that's it! It worked flawlessly!

How do I do the same with ASP.NET Core 1.1?

I have followed documentation which mentions to register it using Microsoft's DI:

services.AddMvc().AddFluentValidation(
    fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());

But that has several problems:

Upvotes: 3

Views: 2003

Answers (1)

Gavin Sutherland
Gavin Sutherland

Reputation: 1686

Appreciate this is an older post but I had a similar issue / confusion using FluentValidation (7.2.1) with Core 2. I couldn't figure out how to get client-side data- attributes onto elements associated with my custom PropertyValidator.

First I created the PropertyValidator implementation ...

public class MyCustomValidator : PropertyValidator
{
    public MyCustomValidator()
        : base("{PropertyName} must do something.") { }

    protected override bool IsValid(PropertyValidatorContext context)
    {
        // Removed for brevity
        return true;
    }
}

... then the IClientModelValidator implementation to supply the client-side data- attributes ...

public class MyCustomPropertyValidator : ClientValidatorBase, IClientModelValidator
{
    public MyCustomPropertyValidator(PropertyRule rule, IPropertyValidator validator)
        : base(rule, validator)
    {
    }

    public override void AddValidation(ClientModelValidationContext context)
    {
        var validator = (MyCustomValidator)Validator;

        MergeAttribute(context.Attributes, "data-val", "true");
        MergeAttribute(context.Attributes, "data-val-whatever", "...");

        // Add other attributes
    }
}   

... then to hook it up use the ConfigureClientsideValidation method when registering Fluent Validation ...

services
.AddMvc()
.AddFluentValidation(cfg =>
{
    cfg.ConfigureClientsideValidation(x =>
    {
        x.Add(typeof(MyCustomValidator), (context, rule, validator) => new MyCustomPropertyValidator(rule, validator));
        // other types ...
    });
    cfg.RegisterValidatorsFromAssemblyContaining<Startup>();
});

Finally, hooking up the JS validation pretty much as you have in the original post.

Upvotes: 1

Related Questions