0

For the same MVC controller action and model I would like to have different validation rules applied based on values on the model and based on what user is making the request.

For the same Controller and the same Action:

  • If a high level user is logged in he should have a different set of validation rules than a lower level user.
  • If the model has been flagged with a certain value then the model will need a different set of validation rules than the models without this flag.

I am interested in any framework that might support this, third party or otherwise.

I posted a similar question on the FluentValidation CodePlex project: https://fluentvalidation.codeplex.com/discussions/439281

A code snipit of what I would like to have from that post:

IValidator GetValidator<T>(object model, Context context)
{
    if (typeof(T) == typeof(MyModel))
    {
        var myModel = (MyModel)model;
        if (myModel.Level == 1 && context.CurrentUser == 2)
        {
            return GetSpecialValidatorForMyModel();
        }
        else
        {
            return GetNormalValidatorForMyModel();
        }
    }
    else
    {
        ...
    }
}

Thanks

Joel
  • 1,033
  • 2
  • 12
  • 23

1 Answers1

0

It looks like I may be able do this with FluentValidation if I inherit from FluentValidationModelValidatorProvider. I can override CreateValidator to achieve what I want. I will also make my own Configure method that uses MyFluentValidationModelValidatorProvider instead.

Joel
  • 1,033
  • 2
  • 12
  • 23