-1

After updating the Jquery.Validation to version: 1.19.2, the error started displaying in the console on page load, it was not throwing errors with the older versions.

This is my code:

var defaultOptions = {
  validClass: '',
  errorClass: 'is-invalid',
  highlight: function(element, errorClass, validClass) {
    $(element).closest("div")
      .removeClass(this.validClass)
      .addClass(this.errorClass);
  },
  unhighlight: function(element, errorClass, validClass) {
    $(element).closest("div")
      .removeClass(this.errorClass)
      .addClass(this.validClass);
  }
};

$.validator.setDefaults(defaultOptions);

$.validator.unobtrusive.options = {
  errorClass: defaultOptions.errorClass,
  validClass: defaultOptions.validClass
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

The error is thrown specifically at : "$.validator.setDefaults(defaultOptions);" line

Sparky
  • 98,165
  • 25
  • 199
  • 285
Martin90
  • 1
  • 6
  • `$.validator` is part of [jQuery Validation](https://jqueryvalidation.org/) not [jQuery Validation Engine](http://posabsolute.github.io/jQuery-Validation-Engine/). Which plugin are you using? – Barmar Nov 06 '20 at 18:10
  • Have you actually loaded either of the plugins? Your snippet just has jQuery, neither plugin. – Barmar Nov 06 '20 at 18:11
  • @Barmar I'm using Jquery Validation - I corrected the tag, thanks! – Martin90 Nov 06 '20 at 20:44
  • @Barmar I downloaded the https://jqueryvalidation.org/ plugin via NuGet on my project. version: 1.19.2 – Martin90 Nov 06 '20 at 20:49
  • If you're still getting the error, it sounds like you still haven't added the ` – Barmar Nov 06 '20 at 20:53
  • @Barmar The script is referenced in the project long time ago. It has been working for over a year, until today, when I updated the version. My thoughts are that it may have something deprecated or changed in this version, but could not figure it out in the documentation. – Martin90 Nov 06 '20 at 20:59
  • @Barmar or might have something to do with the scope of the errorClass and validClass parameters, it started showing a warning that they are declared, but values are never read. – Martin90 Nov 06 '20 at 21:03
  • The error has nothing to do with the parameters. The error means that you've never defined `$.validator`. And that will happen if you haven't loaded the plugin properly. – Barmar Nov 06 '20 at 21:30
  • Check if there were errors when loading the plugin. – Barmar Nov 06 '20 at 21:54
  • Is this an ASP project? You're doing something with Unobtrusive Validation so if that's true, then Unobtrusive automatically constructs and loads the `.validate()` method. Inspect your DOM and verify that the timing of everything is correct. jQuery, then jQuery Validate, then Unobtrusive Validation, then the `.validate()` method, then `.setDefaults()`. – Sparky Nov 06 '20 at 22:09
  • @Sparky Yes, it's an ASP project, I tried with changing the order of the scripts to be loaded, but nothing worked, so I downgraded the version to 1.15.1, and the error was not displaying anymore. – Martin90 Nov 09 '20 at 20:49

1 Answers1

0

Did not find a suitable solution to the issue in version 1.19.2, but downgrading the version back to it's previous version 1.15.1 did the trick.

Martin90
  • 1
  • 6