Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 762089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:54:57+00:00 2026-05-14T15:54:57+00:00

If I have a search object with a list of fields, can I, using

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:54:57+00:00Added an answer on May 14, 2026 at 3:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thougt that sorting a List<T> on the fields I search on would make
I have a ext.form.Combobox object that is dynamically populated by ext.data.JsonStore. The user can
I have a search page where user can submit queries that try to match
I have search in MSDN and I can't figure where are the POST parameters
the div on website below that have search at the bottom and it always
I have a search box that doesn't have a submit button, I need to
I have a search program that will be looking at a database from a
Importing a spreadsheet I have filled a DataTable object with that data and returns
I have written such the code below that ; I have an array list
Let's say I have a domain object that looks like this: @Entity @Indexed public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.