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 8073699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:24:53+00:00 2026-06-05T14:24:53+00:00

I am using an Extended Validation attribute to turn on stricter validation depending on

  • 0

I am using an Extended Validation attribute to turn on stricter validation depending on how I am editing a record.

EG: I have a Parent which has children objects.

When Editing a Child, ModelState.isValid is false, due to the parent missing, e.g a first name, because I am selecting its parent by ID only.

When I edit a Parent — I want these fields to be required, but not when editing e.g a child and the associated parent is part of what makes up the child.

I have the following code:

public class Parent
{
    [Required]        
    public virtual int Id { get; set; }


    [ExtendedValidationRequired(typeof(Parent))]
    public virtual string Name { get; set; }
    ....}

And the following for validation:

public static class ExtendedValidation
{
    private static Dictionary<Type, bool> extendedValidationExemptions = new Dictionary<Type, bool>();


    /// <summary>
    /// Disable extended validation for a specific type
    /// </summary>
    /// <param name="type"></param>
    public static void DisableExtendedValidation(Type type)
    {
        extendedValidationExemptions[type] = true;
    }
    /// <summary>
    /// Clear any EV exemptions
    /// </summary>
    public static void Reset()
    {
        extendedValidationExemptions.Clear();
    }

    /// <summary>
    /// Check if a class should perform extended validation
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static bool IsExtendedValidationEnabled(Type type)
    {
        if (extendedValidationExemptions.ContainsKey(type))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}






[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]

public class ExtendedValidationRequiredAttribute : RequiredAttribute
{


    private Type _type;
    // Summary:
    //     Initializes a new instance of the System.ComponentModel.DataAnnotations.RequiredAttribute
    //     class.
    public ExtendedValidationRequiredAttribute(Type type)
    {
        _type = type;
    }



    // Summary:
    //     Checks that the value of the required data field is not empty.
    //
    // Parameters:
    //   value:
    //     The data field value to validate.
    //
    // Returns:
    //     true if validation is successful; otherwise, false.
    //
    // Exceptions:
    //   System.ComponentModel.DataAnnotations.ValidationException:
    //     The data field value was null.
    public override bool IsValid(object value)
    {
        if (ExtendedValidation.IsExtendedValidationEnabled(_type))
        {
            return base.IsValid(value);
        }
        else
        {
            return true;
        }


    }




}

Is there any way I can get around passing the type explicitly into the validator? I want to be able to enable/disable these validation attributes programmatically.

For example, if I am editing a Child, but a Parent’s [Required] fields would cause me to have problems, I would disable typeof(Parent) in the extendedValidator, and then Validation should work OK. I am just worried about performance impact with all the typeof() going on.

My problem that this attempts to address is when I am editing a class such as

public class Child
{

   [Required]
   public virtual int id {get;set;}

   [Required]
   public virtual Parent parent {get;set;}

   ....
}

My form just has the Parent ID (that is all that is needed in my EF or hibernate app), but it will not validate unless the Parent passed in the form has all of the required fields — which is rather wasteful.

  • 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-06-05T14:24:55+00:00Added an answer on June 5, 2026 at 2:24 pm

    You could override ValidationAttribute.IsValid Method (Object, ValidationContext)

    protected virtual ValidationResult IsValid(
        Object value,
        ValidationContext validationContext
    )
    

    And access members of ValidationContext Class

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

Sidebar

Related Questions

Server side extjs form validation using zend framework I have extended Zend_Form class to
I have an extended RelativeLayout which, when programmatically positioned and sized using RelativeLayout.LayoutParams ,
I extended the functionality of a line object using a class which takes a
I'm using the Prototype.js (from www.prototypejs.org) library to create classes which are extended by
I have had some difficulties in using extended classes in rails, in particular extending
I have an existing .NET 3.5 based framework that is extended using custom plugins.
I have an Entity class that I have extended using the partial class syntax
How can I develop applications in C# that can be further extended using DLL
Using js inside an iframe to ask for extended permissions: FB.ensureInit(function() { FB.Connect.showPermissionDialog(publish_stream, submit_post);
How can I find extended ASCII characters in a file using Perl? Can anyone

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.