Solved

MVC - Dependent Required Validation Attribute Help

Posted on 2014-04-17
2
502 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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