Model Validation / ASP.NET MVC 3 - Conditional Required Attribute
Asked Answered
O

4

21

I'm having trouble with my ASP.NET MVC 3 application. I have 2 propertiesin my model whereby I only want 1 of them required in my view based on whichever one is empty. So for example, if I enter a phone number then email is no longer required and vice versa, but if I leave both empty, then either 1 should be required, below is my model:

[Display(Name = "Contact Phone Number:")]
[MaxLength(150)]
public string ContactPhoneNumber { get; set; }

[Display(Name = "Contact Email Address:")]
[MaxLength(100)]
public string ContactEmailAddress { get; set; }

Would I need to create a custom attribute to validate my model and if so, how would I achieve this?

Omphale answered 23/11, 2011 at 13:18 Comment(0)
F
23

You can implement IValidatableObject on your class and provide a Validate() method that implements your custom logic. Combine this with custom validation logic on the client if you prefer to ensure that one is supplied. I find this easier than implementing an attribute.

public class ContactModel : IValidatableObject
{
   ...

   public IEnumerable<ValidationResult> Validate( ValidationContext context )
   {
        if (string.IsNullOrWhitespace( ContactPhoneNumber ) 
            && string.IsNullOrWhitespace( ContactEmailAddress ))
        {
             yield return new ValidationResult( "Contact Phone Number or Email Address must be supplied.", new [] { "ContactPhoneNumber", "ContactEmailAddress" } );
        }
   }
}

To get everything working at client side you'll need to add the following script to your view:

<script type="text/javascript">
$(function() {
    $('form').validate(); 
    $('form').rules('add', { 
        "ContactPhoneNumber": { 
            depends: function(el) { return !$('#ContactEmailAddress').val(); } 
        } 
    });
});
</script>
Frazil answered 23/11, 2011 at 13:27 Comment(4)
how about client side validation?Forensics
Probably hook into the jQuery validate plugin by adding a custom rule. $('form').validate(); $('form').rules('add', { "ContactPhoneNumber" : { depends: function(el) { return !$('#ContactEmailAddress').val(); } } } ); or similarFrazil
I've poked around a little bit but haven't found an easy way to create an attribute that hooks into the automated client-side validation framework. If you're interested, you trace how the CompareAttribute is handled and see if you can extend the framework with something similar.Frazil
I took the liberty and added the client side code to your answer. +1Forensics
E
16

Annotation-based conditional validation can be defined using ExpressiveAnnotations:

[RequiredIf("ContactPhoneNumber == null",
    ErrorMessage = "At least email or phone should be provided.")]
public string ContactEmailAddress { get; set; }

[RequiredIf("ContactEmailAddress == null",
    ErrorMessage = "At least email or phone should be provided.")]
public string ContactPhoneNumber { get; set; }
Extravagancy answered 18/5, 2014 at 0:24 Comment(0)
F
1

Here is a MSDN blog entry about conditional validations: http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx

Forensics answered 23/11, 2011 at 13:50 Comment(0)
S
1

I know you already have a solution, but I had a similar situation, so maybe my solution will prove helpful to someone else. I implemented a custom attribute with client-side validation. Here is my blog post: http://hobbscene.com/2011/10/22/conditional-validation/

Sepoy answered 22/3, 2012 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.