Forcing a revalidate on mvc3 unobtrusive remote validation

不问归期 提交于 2019-12-18 13:15:13

问题


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?


回答1:


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');


来源:https://stackoverflow.com/questions/5618247/forcing-a-revalidate-on-mvc3-unobtrusive-remote-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!