Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: NullReferenceException using FluentValidation in MVC3

$
0
0

Got it - thanks.

Looking at this, it appears the issue was fixed last month in a patch, however I haven't yet got round to doing a new deployment since then. If you download and build from source, then the error should go away.

As expected, the problem was caused by using RuleFor(t => t). Doing this is not supported (with FluentValidation, rules should be defined against properties, not against the object itself). However, although this isn't supported it shouldn't cause a crash. Someone noticed this issue last month and kindly provided a patch for it.

With the TestIsDefined rule, I'd recommend associating it with one of the properties, eg TestID:

 

public TestValidator()
{
    RuleFor(t => t.TestID).Must(TestIsDefined).WithMessage("User must select existing customer or provide at least a new customer name");
}privatebool TestIsDefined(TestModel model, int? testId)
{return (TestID.HasValue || (string.IsNullOrWhiteSpace(model.TestName) && string.IsNullOrWhiteSpace(model.TestDescription)));
}

 

 

However, if you really want to use RuleFor(t=>t) then you'll need to call .OverridePropertyName as part of the rule (as FV won't be able to automatically determine which property to associate the rule with).

Jeremy


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>