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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:49:21+00:00 2026-06-01T05:49:21+00:00

So, I took some code from this Microsoft provided Example which allows me to

  • 0

So, I took some code from this Microsoft provided Example which allows me to use the jquery validate unobtrusive library to parse validation error message returned from my server and display them in the UI. They have a video demonstrating this. So, here is the piece of Javascript code I’m using:

   $.validator.addMethod("failure", function () { return false; });
    $.validator.unobtrusive.adapters.addBool("failure");
    $.validator.unobtrusive.revalidate = function (form, validationResult) {
        $.removeData(form[0], 'validator');
        var serverValidationErrors = [];
        for (var property in validationResult) {
            //var elementId = property.toLowerCase();
            var item = form.find('#' + property);
            if (item.length < 1) { item = form.find('#' + property.replace('.', '_')); }
            serverValidationErrors.push(item);
            item.attr('data-val-failure', validationResult[property].join(', '));
            jQuery.validator.unobtrusive.parseElement(item[0]);
        }
        form.valid();
        $.removeData(form[0], 'validator');
        $.each(serverValidationErrors, function () {
            this.removeAttr('data-val-failure');
            jQuery.validator.unobtrusive.parseElement(this[0]);
        });
    };

So then after a AJAX form post in the handle error function I would do something like this:

$.validator.unobtrusive.revalidate(form, { 'PhysicalAddress.CityName': ['You must select a valid city'] });

Where PhysicalAddress.CityName is the name of my viewmodel property and html input field. So, it knows to put the validation message next to the correct html element.

This works 1 time. Then when they hit submit again and my code calls the unobtrusive.revalidate method again.. it doesnt work. It only shows the validation message one time then after that the validation message disappears for good.

Does anyone have any idea as to why this might be happening?.. I stepped through the revalidate method and no errors were thrown and everything seems like it should work.. but the unobtrusive library for some reason is not re-binding the validation error message.

Thanks

  • 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-01T05:49:22+00:00Added an answer on June 1, 2026 at 5:49 am

    Probably this behavior depends on a known problem of the jQuery validation plugin: dynamically adding new validation rules for elements works just once! Further attempts are rejected because the plugin think they are a duplicated attempt to define the already defined rules.

    This is the reason why the $.validator.unobtrusive.parse doesn’t work when you add newly created content (when for instance you add a new row to a collection of items). There is a patch for the $.validator.unobtrusive.parse that you might try to apply also to the revalidate function….but it is better to rewrite it from scratch in a different way. The revalidate function usse the validation plugin just to place at the right place all validation errors, then it tries to reset the state of the validation plugin. However, deleting the validator object from the form is not enough to cancel all job done since there is another object contained in the form.data('unobtrusiveValidation'), where form is a variable containing the form being validated…This data are not reset by the revalidate function…and CANNOT be reset since resetting them would cause the cancellation of ALL client side validation rules.

    Maybe this problem has been solved in the last version of the validation plugin, so try to update to the last version with nuget.

    If this doesn’t solve your issue I can pass you an analogous function implemented in a completely different way(it mimics what the server does on the server side to show server side errors). It will be contained in the upcoming version of the Mvc Controls toolkit. However, if you give me a couple of days (I will be very busy for 2 days) I can extract it from there with its dependencies so you can use it. Let me know if you are interested.

    Below the code I promised. It expects an array whose elements are:

    {
        id:id of the element in error
        errors:array of strings errors associated to the element
    }
    

    It accepts several errors for each element but just display di first one for each element
    id is different from the name because . [ ] an other special char are replaced by _

    You can transform name into id on the sever with

    htmlName.Replace('$', '_').Replace('.', '_').Replace('[', '_').Replace(']', '_');
    

    or on the client in javascript with:

    name.replace(/[\$\[\]\.]/g, '_');
    
    function remoteErrors(jForm, errors) {
        //////////
        function inner_ServerErrors(elements) {
            var ToApply = function () {
                for (var i = 0; i < elements.length; i++) {
                    var currElement = elements[i];
                    var currDom = $('#' + currElement.id);
                    if (currDom.length == 0) continue;
                    var currForm = currDom.parents('form').first();
                    if (currForm.length == 0) continue;
    
                    if (!currDom.hasClass('input-validation-error'))
                        currDom.addClass('input-validation-error');
                    var currDisplay = $(currForm).find("[data-valmsg-for='" + currElement.name + "']");
                    if (currDisplay.length > 0) {
                        currDisplay.removeClass("field-validation-valid").addClass("field-validation-error");
                        replace = $.parseJSON(currDisplay.attr("data-valmsg-replace")) !== false;
                        if (replace) {
                            currDisplay.empty();
                            $(currElement.errors[0]).appendTo(currDisplay);
                        }
                    }
                }
            };
            setTimeout(ToApply, 0);
        }
        /////////
        jForm.find('.input-validation-error').removeClass('input-validation-error');
        jForm.find('.field-validation-error').removeClass('field-validation-error').addClass('field-validation-valid');
        var container = jForm.find("[data-valmsg-summary=true]");
        list = container.find("ul");
        list.empty();
        if (errors.length > 0) {
            $.each(errors, function (i, ival) {
                $.each(ival.errors, function (j, jval) {
                    $("<li />").html(jval).appendTo(list);
                });
    
            });
            container.addClass("validation-summary-errors").removeClass("validation-summary-valid");
            inner_ServerErrors(errors);
            setTimeout(function () { jForm.find('span.input-validation-error[data-element-type]').removeClass('input-validation-error') }, 0);
        }
        else {
            container.addClass("validation-summary-valid").removeClass("validation-summary-errors");
        }
    }
    function clearErrors(jForm) {
        remoteErrors(jForm, []);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I took some code from my C# Windows form app which uses the Application
I have some socket connection code that makes use of boost::asio which reads from
I took some code from some questions here in SO as well as some
I took a method from this post (accepted answer code) that serializes object so
I took some code from the internet that searches data entered in a table
I recently took some code from Delphi 2007 and upgraded it to Delphi 2009.
When looking through some code I took over, I came across this line: my
I took some code here: Check if role consists of particular user in DB?
I took the following code from the examples page on Asio class tcp_connection :
I use this function to read information from xls (2003) file var connectionString =

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.