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

New Post: Object not Initialized

$
0
0

Ok so I have a rather interesting issue....

I have a model with some properties that are optional (but required in certain cases) this model is a Entity Framework model just for informational purposes but looks something like this.

public class MyClass
{
   int Prop1 {get;set;}

   int Prop2 {get;set;}

   MyOtherClass Navigation {get;set;}
}

public class MyOtherClass
{
  bool IsProp1Required {get;set;}
}

what I want to do is create a rule for prop1 in its validator that only makes it required when the IsProp1Required is set to true.

I am using this in an MVC form, that has a hidden input for Navigation (of MyClass) and has the NAvigation property populated before being passed to the form

However on submit I am getting a null reference exception, my rule looks basically like this.

public class MyClassValidator:AbstractValidator<MyClass>
{
public MyClassValidator()
{
   RuleFor(model =>model.Prop1).NotEmpty().When(model=>model.Navigation.ISProp1Required == true);
}
}

thats it in a nutshell (I am typing this from memory,so excuse if the code doesn't compile)

When I submit the Navigation property is null which means the when check throws the exception.

Any thoughts?


Viewing all articles
Browse latest Browse all 1917

Trending Articles



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