Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MVC - Dependent Required Validation Attribute Help

Posted on 2014-04-17
2
Medium Priority
?
540 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview

886 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