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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:29:03+00:00 2026-05-23T10:29:03+00:00

If i have [Required(AllowEmptyStrings = true)] declaration in my view model the validation is

  • 0

If i have [Required(AllowEmptyStrings = true)] declaration in my view model the validation is always triggered on empty inputs. I found the article which explains why it happens. Do you know if there is a fix available? If not, how do you handle it?

  • 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-23T10:29:04+00:00Added an answer on May 23, 2026 at 10:29 am

    Note: I’m assuming you have AllowEmptyStrings = true because you’re also using your view model outside of a web scenario; otherwise it doesn’t seem like there’s much of a point to having a Required attribute that allows empty strings in a web scenario.

    There are three steps to handle this:

    1. Create a custom attribute adapter which adds that validation parameter
    2. Register your adapter as an adapter factory
    3. Override the jQuery Validation function to allow empty strings when that attribute is present

    Step 1: The custom attribute adapter

    I modified the RequiredAttributeAdapter to add in that logic:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    namespace CustomAttributes
    {
        /// <summary>Provides an adapter for the <see cref="T:System.Runtime.CompilerServices.RequiredAttributeAttribute" /> attribute.</summary>
        public class RequiredAttributeAdapter : DataAnnotationsModelValidator<RequiredAttribute>
        {
            /// <summary>Initializes a new instance of the <see cref="T:System.Runtime.CompilerServices.RequiredAttributeAttribute" /> class.</summary>
            /// <param name="metadata">The model metadata.</param>
            /// <param name="context">The controller context.</param>
            /// <param name="attribute">The required attribute.</param>
            public RequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
                : base(metadata, context, attribute)
            {
            }
            /// <summary>Gets a list of required-value client validation rules.</summary>
            /// <returns>A list of required-value client validation rules.</returns>
            public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
            {
                var rule = new ModelClientValidationRequiredRule(base.ErrorMessage);
                if (base.Attribute.AllowEmptyStrings)
                {
                    //setting "true" rather than bool true which is serialized as "True"
                    rule.ValidationParameters["allowempty"] = "true";
                }
    
                return new ModelClientValidationRequiredRule[] { rule };
            }
        }
    }
    

    Step 2. Register this in your global.asax / Application_Start()

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
    
            DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RequiredAttribute),
              (metadata, controllerContext, attribute) => new CustomAttributes.RequiredAttributeAdapter(metadata,
                controllerContext, (RequiredAttribute)attribute)); 
    
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    

    Step 3. Override the jQuery “required” validation function

    This is done using the jQuery.validator.addMethod() call, adding our custom logic and then calling the original function – you can read more about this approach here. If you are using this throughout your site, perhaps in a script file referenced from your _Layout.cshtml. Here’s a sample script block you can drop in a page to test:

    <script>
    jQuery.validator.methods.oldRequired = jQuery.validator.methods.required;
    
    jQuery.validator.addMethod("required", function (value, element, param) {
        if ($(element).attr('data-val-required-allowempty') == 'true') {
            return true;
        }
        return jQuery.validator.methods.oldRequired.call(this, value, element, param);
    },
    jQuery.validator.messages.required // use default message
    );
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to replace validation message for all model properties that have [Required]
When evaluating dojo.require statements, dojo tracks which modules and resources have been required and
Is if almost always required to have thread syncing (i.e. use of mutex, semaphores,
I have, let's say, this simple class: public class User { [Required(AllowEmptyStrings = false,
I have a required validation setup on a textbox, but I also have to
I have a group of text boxes that have required field validation hooked up
In the past, some of my projects have required me to create a movie
Unfortunately we do not have the required DCOM access to the SQL Server mentioned
After spliting a tab delimited file I have my required values in a string
I have added the required assemblies and registered the NVelocityViewFactory in global.asax.cs page but

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.