Monday, July 26, 2010

"$.validator.methods[method] is undefined"

July 26, 2010 Posted by Jason Irwin , , 2 comments

I’ve started developing a new project and have recently been getting into ASP.NET MVC and JQuery a lot more. To make my life easier I decided to use the JQuery Validation libraries to perform my client side validation. I created a simple registration form with three fields – username, password, confirm password. The following is the code I am using:

 

image

 

The code uses a remote call to the IsLoginAvailable action of the user controller which in turn checks if the entered username has already been taken and wraps the response in a JsonResult. This appeared to work fine (validating the field when focus was lost) but upon submitting the form I received the following error:

 

"$.validator.methods[…] is undefined"

 

or

 

"$.validator.methods[method] is undefined"

 

After debugging with Firebug I found that the validation was failing on a "data" method. Looking at the code I realized I was specifying the username to pass via the data section in the remote call – this was something I picked up in the examples online and assumed I too would need to use – shame on me!

 

I removed the data section from my remote call and lo-and-behold everything worked as it should. I was surprised to see that my #username value was passed correctly to the controller action and the check was performed successfully. Better still, upon submitting the form I no longer receive the "Error: ‘$.validator.methods[…]’ is null or not an object" error...now to figure out why I would or wouldn’t need the data section!

2 comments: