Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MVC - Dependent Required Validation Attribute Help

Posted on 2014-04-17
2
Medium Priority
?
531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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