views:

97

answers:

3

If I have a search object with a list of fields, can I, using the System.ComponentModel.DataAnnotations namespace, set it up to validate that at least one of the fields in the search is not null or empty? i.e All the fields are optional but at least one should always be entered.

A: 

If you want to do complex validation against any .Net class, without litering them with annotations, look at FluentValidation, or for .Net 2.0, FluentValidation for 2.0

Daniel Dyson
Daniel, thanks for the answer but as we have been using the annotations successfully thus far in the project and it has been very good for what we have needed, I want to try to stick to this method of validation. This is the first real stumbling point thus far and I'm hoping someone may have a nice solution for me! :)
Boob
No worries. When you get time, check out the links. You will find the Fluent approach to be intuitive and easy to use. And very powerful.
Daniel Dyson
Will do, thanks again
Boob
A: 

In addition to Daniel's answer, you can also look for Validation Application Block (Enterprise Library 5.0). VAB 5.0 allows integration with DataAnnotations, but also allows for much more complex validation scenario's.

Steven
+2  A: 

I'd create a custom validator for this - it won't give you client side validation, just server side.

Note that for this to work, you'll need to be using nullable types, as value types will default to 0 or false:

First create a new validator:

using System.ComponentModel.DataAnnotations;
using System.Reflection;

// This is a class-level attribute, doesn't make sense at the property level
[AttributeUsage(AttributeTargets.Class)]
public class AtLeastOnePropertyAttribute : ValidationAttribute
{
  // Have to override IsValid
  public override bool IsValid(object value)
  {
    //  Need to use reflection to get properties of "value"...
    var typeInfo = value.GetType();

    var propertyInfo = typeInfo.GetProperties();

    foreach (var property in propertyInfo)
    {
      if (null != property.GetValue(value, null))
      {
        // We've found a property with a value
        return true;
      }
    }

    // All properties were null.
    return false;
  }
}

You can then decorate your models with this:

[AtLeastOneProperty(ErrorMessage="You must supply at least one value")]
public class SimpleTest
{
    public string StringProp { get; set; }
    public int? Id { get; set; }
    public bool? BoolProp { get; set; }
}

Then when you call ModelState.IsValid your validator will be called, and your message will be added to the ValidationSummary on your view.

Note that you could extend this to check for the type of property coming back, or look for attributes on them to include/exclude from validation if you want to - this is assuming a generic validator that doesn't know anything about the type it's validating.

Zhaph - Ben Duguid
+1 Nice. I didn't know adding class level attributes was possible with DataAnnotations.
Steven
This looks really nice, I'll get back to this before the end of the week and give your answer a go. I'll let you know how it turns out! thanks
Boob