Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing a revalidate on mvc3 unobtrusive remote validation

It's a classic login flow. The user can choose between 'new user' or 'existing user'. If the user is new, the name in the login box should validate against the server to see if the username is unique, if it's an existing user this check will be skipped since we expect the username to be taken already (ofcourse).

I added a [Remote] attribute on the viewmodel and added the radiobutton for new/exiting user as 'additional fields'. This way the remote validation will just return true if it's an existing user, and check the database if it's not, to see if the username is taken.

This works great, except when the user decides to change the radiobutton (new/existing) after entered a username (and the remote validation has run). Since remote validation is only automatically run when the username changes (that's the property with [Remote] attribute), changing the radiobutton alone, will not have it run again.

So my problem is, how can i force the remote validation to run again? I tried the usual hacks by triggering a change/focus/blur event on the username input field, but the call is not triggered. I considered adding a similar [Remote] on the radiobutton, but that would really complicate things with two equal looking error messages, placed at the same absolute position.

Is there any way to trigger a revalidation?

like image 468
Per Hornshøj-Schierbeck Avatar asked Apr 11 '11 07:04

Per Hornshøj-Schierbeck


1 Answers

I believe that jquery validation can be triggered using $("#formID").validate()

Some more options can be found in the docs: http://docs.jquery.com/Plugins/validation, and at Brad Wilson's blog (you can also find some info in the comments)

Have you looked into the Data attributes that the input has, maybe it's cached? something like this.

EDIT: Just for clarification, this is how i got it to work

$('#UserName').removeData('previousValue');
$('form').validate().element('#UserName');
like image 180
Linkgoron Avatar answered Dec 10 '22 06:12

Linkgoron