Skip to content

Custom view model validators

Alexanderius edited this page Sep 15, 2019 · 3 revisions

Custom view model validators

The model validators are working the same as binders.

To use custom model validator you should register it using HttpModelHandler.RegisterModelValidator, it will be added to validators pipeline.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ...

        HttpModelHandler.RegisterModelValidator<MyModelValidator>();

        app.UseSimplifyWeb();
    }
}

Validator should be deriver from IModelValidator interface.

public class MyModelValidator : IModelValidator
{
    public void Validate<T>(T model)
    {
        // Validation logic
    }
}

If you just want to use your validator without default validators, then you should clear validators list first.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ...
        HttpModelHandler.ModelValidatorsTypes.Clear();
        HttpModelHandler.RegisterModelValidator<MyModelValidator>();

        app.UseSimplifyWeb();
    }
}

<< Previous page

Clone this wiki locally