Solved

MVC - Dependent Required Validation Attribute Help

Posted on 2014-04-17
2
493 Views
Last Modified: 2014-04-24
Hello all,

I have two properties in my view model that are nullable integers such as:

public int? yearending
public int? quarter

I need a ValidationAttribute that if one of these values is not null then the other value has to also be not null.   Both ways I need this so if yearending is not null then quarter is required otherwise if quarter is not null yearending is required.
0
Comment
Question by:sbornstein2
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40007645
There isn't an out-of-the-box validator that will do that, so you'll need to roll your own. Something along these lines should do it (untested!):
public class CheckNullAttribute : ValidationAttribute
{
    public CheckNullAttribute(string targetProperty, string checkAgainstProperty)
    {
        this.TargetProperty = targetProperty;
        this.CheckAgainstProperty = checkAgainstProperty;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var target = validationContext.ObjectInstance.GetType().GetProperty(TargetProperty);
        int? targetValue = target.GetValue(validationContext.ObjectInstance, null) as int?;

        var checkAgainst = validationContext.ObjectInstance.GetType().GetProperty(CheckAgainstProperty);
        int? checkAgainstValue = checkAgainst.GetValue(validationContext.ObjectInstance, null) as int?;

        if ((targetValue.HasValue && !checkAgainstValue.HasValue) || (checkAgainstValue.HasValue && !targetValue.HasValue))
            return new ValidationResult("Some message");

        return ValidationResult.Success;
    }

    public string TargetProperty { get; set; }
    public string CheckAgainstProperty { get; set; }
}

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 40021687
awesome thank you.  Sorry it took me so long to accept your solution appreciate it.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now