Solved

MVC - Dependent Required Validation Attribute Help

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
write html in textarea and record it into a database table 3 41
asp.net mvc5 6 33
EF5: Update Model from Database not working 3 83
How does this modal gets closed? 6 41
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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