Multiple Property validation and Dynamic Property Validation in MVC Fluent Validation.

Jun 19, 2013 at 3:45 PM
Here is my mvc model class
public class contactpreference
   public string notification {get;set;}
   public bool textmessage {get;set;}
   public bool email {get;set;}
I need a fluent validation rule which says

if( notification.equals("true")} //its string and not bool thats why "true"
if( !textmessage && !email)
  show error message ( Please select either textmessage or email as your option);

Also I have one more scenario where I have plenty of radio buttons on my page which are shown to user dyanamically as below.
public class example 
   public string option1 {get;set;}
   public string option2  {get;set;}
   public string option3  {get;set;}
   public string option4  {get;set;}
   public string option5  {get;set;}
   public string option6  {get;set;}
Each option is shown as Radiobutton on my MVC View dynamically which means on pageload user will first see Option1 Radiobutton without any selection. if he select "Yes" then user will be shown Option2, Option3 and Option4

if Option4 is selected Option5 and Option6 is displayed.

I want to show validation messages such as " Please select either "Yes" or "No"". only once for all the options which is not been selected.

My current approach shows above message for each radio button. I want to avoid duplicate messages but also wanted to make sure atleast one message is displayed if any of the radiobutton "rendered"/"visible" is unselected.
Jun 20, 2013 at 9:21 AM
For your first scenario, it'd probably be simpler to use Custom rather than associating the rule with a single property (see this page in the docs)

For your second scenario, I'm not sure I really understand what you're trying to do...perhaps you could provide more detail?
Jun 21, 2013 at 2:49 PM
Thanks Jeremy, I used Custom for first scenario and it works great !!!

For second scenario : -

I have RadioButton Displayed on my View on page Load.

Option1 : Yes No => if they select Yes then I have to dynamically show them again RadioButton for Option2 and Option3. and OPtion4

So here Option 2, 3 & 4 are required selection if they have selected OPtion1.

Similary if selected OPtion4 then OPtion5 & 6 are required.

I can write rules and set Generic required message for each unselected radiobuttons. But at the end the view it will show multiple generic required messages for all the unselected options. that is wierd to have same messages 3 -4 times.

I did found out solution as follows: - I implemented IValidatorInterceptor
public FluentValidation.Results.ValidationResult AfterMvcValidation(System.Web.Mvc.ControllerContext controllerContext, ValidationContext validationContext, FluentValidation.Results.ValidationResult result)
            var distinctList = result.Errors.GroupBy(x => x.PropertyName)
                            .Select(g => g.First())

            if (distinctList.Count > 0)
                foreach (ValidationFailure error in distinctList)
            return result;

        public ValidationContext BeforeMvcValidation(System.Web.Mvc.ControllerContext controllerContext, ValidationContext validationContext)
            return validationContext;
I clear out the duplicate error messages. Not sure if that is the best way to do it but it is working.