强制重新验证 mvc3 非侵入式远程验证
这是一个经典的登录流程。用户可以选择“新用户”或“现有用户”。如果用户是新用户,则登录框中的名称应根据服务器进行验证,以查看用户名是否唯一,如果是现有用户,则将跳过此检查,因为我们希望用户名已被使用(当然)。
我在视图模型上添加了一个 [Remote] 属性,并为新/退出用户添加了单选按钮作为“附加字段”。这样,如果是现有用户,远程验证将返回 true,如果不是,则检查数据库,看看用户名是否被占用。
这非常有效,除非用户在输入用户名后决定更改单选按钮(新的/现有的)(并且远程验证已运行)。由于远程验证仅在用户名更改时自动运行(这是具有 [Remote] 属性的属性),因此单独更改单选按钮不会使其再次运行。
所以我的问题是,如何强制远程验证再次运行?我通过在用户名输入字段上触发更改/焦点/模糊事件来尝试常见的黑客方法,但未触发调用。我考虑在单选按钮上添加类似的 [Remote],但这确实会使事情变得复杂,因为两条看起来相同的错误消息放置在相同的绝对位置。
有什么办法可以触发重新验证吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信可以使用 $("#formID").validate() 触发 jquery 验证。
可以在文档中找到更多选项: http://docs.jquery.com/Plugins/validation,以及Brad Wilson 的博客(您还可以在评论中找到一些信息)
您是否查看过输入具有的数据属性,也许它已缓存?类似于 这个。
编辑:只是为了澄清,这就是我让它工作的方式
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