问题标签 [unobtrusive-validation]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
asp.net-mvc-3 - 在 ASP.NET MVC 3 中使用不显眼的验证,当表单无效时如何采取措施?
我有一个带有 jQuery UI 选项卡的页面,第一个选项卡上有一个必填字段。验证有效,如果用户没有输入值,验证消息会显示在字段旁边。但是,如果用户不在第一个选项卡上,则该字段无效并不明显。
在这种情况下,我想显示第一个选项卡,但我不确定如何为“无效”事件注册回调(如果有的话)。
我还考虑在选项卡上方显示验证摘要,无论用户在哪个选项卡上都可以看到该摘要,但我更喜欢将用户带到该字段。
我试过浏览 jquery.validate.unobtrusive.js 文件,它似乎扩展了 jquery.validate.js。查看 jquery.validate 的文档,我看到有一个invalidHandler
选项可以传递给validate 方法,但我不知道如何使用它,因为该方法是由 jquery.validation.unobtrusive 调用的。
我在这里感觉有点失落,所以任何帮助将不胜感激!
编辑
我从来没有想出答案,但确实想出了一个解决方法,我在下面发布了。不过,我希望有人用更好的方法来回答这个问题。
localization - ASP.NET MVC3 不显眼的 jQuery 验证消息本地化
我坚持使用 ASP.NET MVC 3 jQuery 不显眼的验证消息本地化。特别是“数字”验证。如果我在模型输入 html 中有一个数字属性,则使用 data-val-number 属性呈现,其值为“字段数量必须是一个数字”。我如何本地化这个字符串。使用数据注释属性,定义本地化消息没有问题。但是对于数字验证,我不必指定任何属性。
那么,如何才能通过不显眼的验证生成本地化的验证消息呢?
asp.net-mvc - asp.net mvc 3中的远程验证
我们正在从 mvc2 升级到 mvc3,并且在远程验证功能方面确实遇到了问题。这就是 web.config 的 appsetting 的样子
这就是我的模型和相关元数据的样子
我已确保所需的 jquery 文件以正确的顺序加载到页面上。当我检查生成的 html 代码时,我没有找到为远程验证生成的任何 HTML5 字段(那里存在正则表达式和所需验证的字段)
我不知道可能是什么问题。我创建了一个示例 mvc3 项目,在该项目中一切似乎都工作正常,但是当我尝试在我的应用程序中实现它时它就不起作用。非常感谢任何帮助。
谢谢
asp.net-mvc-3 - ASP.Net MVC 3 不显眼的验证不适用于部分视图
我已经设置了一个包含自己的表单标签的局部视图,如下所示:
我使用 @Html.Action("Create") 在页面上呈现它(它是表格的一部分,因此是 <tr> 标签。
出于某种奇怪的原因,我的客户端验证不起作用,我首先在发布时看到错误。
部分视图和客户端验证有什么特别之处吗?
我已包含以下脚本:
编辑
我只是试着把这个脚本扔到页面上:
它警告“真”,所以客户端验证脚本肯定在那里,并且由于某种原因它没有检查有问题的字段:-/
编辑 2
我已将页面的整个源代码(HTML + JS)上传到 pastebin: http: //pastebin.com/GvqLW495
asp.net-mvc-3 - ASP.Net MVC 3 - Client side unobtrusive validation for Editor Templates
I am new to ASP.Net MVC 3, facing some issues while trying to implementing client side unobtrusive validation for a editor template I have created for showing date in a custom way.
UI
I need to show date in a three texbox UI format as
I have put up a EditorTemplate for displaying the date in three parts as
Model
I have to bind a Date of Birth field which is a property in a subobject of my model to this property, in this structure
Clientside Validation
I have put up a custom validation attribute which implements IClientValidatable as
I am trying to emit the element names of Day, Month & Year textboxes here to client side validation elements, so that I will write a client side jquery validation method and adapter later which would consume those elements and do the validation at the client side.
View
Now, to use this editor template, I put in View the following lines
Added all relevant jquery validation files in the view as references
Questions
- This is not emitting out the unobtrusive javascript validation attributes in the html, though I have implemented IClientValidatable. For testing purpose when I put the same attribute (DateTimeClientValidation) on another property in the model which was not using this editor template, then it emitted out those validation attributes, it is not emitting it out only for this editor template. Where could have I gone wrong ?
- Regarding Validation Message span for the editor template, is it right that I put it in View only or should I put it directly in the editor template (@Html.ValidationMessageFor(m => m.MySubModel.DateofBirth))
- In this example, am I right in the design, I have put in DateTimeClientValidationAttribute, which actually is an attribute I put on model, but this component knows a bit about UI (since it is trying to emit out the Day, Month & Year elements name to the client), this makes Model know a bit about View, am I breaking any design principles here ?
- In DateTimeClientValidationAttribute, I am trying to emit out the day, month & year elements names to the client, so that the client script can do validations on it. But since the model property DateofBirth is in a subobject, the actual element name in the script is MySubObject.DateOfBirth, which makes the Day textbox name to be MySubObject.DateofBirth.Day, how can I find that fully qualified model name in the GetClientValidationRules method, so that I can emit out the name to the client ?
Thanks for being patient to read out all this, and for the answers
validation - ASP.Net MVC3 - 我们可以在 EditorTemplate 级别应用自定义客户端验证吗
如果 Model 中的某个字段绑定到 EditorTemplate,如果 Model 中的字段使用验证属性修饰,是否会为 EditorTemplate 生成 javascript 不显眼的 data-val-* 参数。
此外,如果我应用自定义客户端验证,通过使用 IClientValidatable 实现验证属性,并使用该自定义属性装饰此字段,它是否会为客户端中的 EditorTemplate 生成 data-val-* 属性,用于客户端规则我添加自定义验证属性?
我似乎没有让它工作,它不会为我发出客户端属性。
这是一个更简单的部分,也是我之前的帖子的一部分
asp.net-mvc-3 - MVC 3 Razor - 不显眼的验证不起作用
谁能看到为什么我的验证不起作用?目前它只是在插入时发布并失败,因为数据插入不允许空值,而不是在客户端捕获它并显示所需的字段消息。
查看
http://pastebin.com/4grwD02i
控制器
http://pastebin.com/jdbYk8tR
布局
http://pastebin.com/AbQ9xYLG
应用设置
~~更新~~
型号
http://pastebin.com/FJkPgKsX
我只是为我的 DAL 使用 Linq to SQL DBML 文件,所以,不,我没有对属性进行任何装饰。我可以这样做并仍然使用 DBML 吗?
unobtrusive-validation - 嵌套(集合)属性的 mvc 客户端验证
我正在使用带有 jquery 不显眼验证的 asp.net mvc 3。我最近从标准 DataAnnotations 更改为 FluentValidation 并且效果很好。
我选择 FluentValidation 的主要原因是需要验证我的视图模型上的嵌套属性(但我发现使用它还有其他很酷的原因),有点像这样(不要介意访问器,这是伪的):
使用 FluentValidation 我为 Vm 和 ElementVm 制作了一个验证器,我的单元测试是绿色的,表明服务器端验证正在工作。
客户端,“Prop”和“AnotherProp”正在工作-我的验证规则也按预期在客户端运行(就像使用 DataAnnontation 一样),但是我所有的元素根本没有得到任何客户端验证-我检查了dom 并且可以看到所有 data-val、data-required 等属性都丢失了。
我尝试了不同的方法来在我的视图中生成 html,但是“Prop”和“AnotherProp”是使用 Html.TextBoxFor(m => m.Prop) 生成的,而我的元素是部分生成的——这就是问题开始。如果我选择 Html.TextBoxFor(m => m.Value) 我所有的元素文本框都将具有相同的名称/id,所以我也尝试使用 Html.TextBox(Model.Id) 生成唯一的 id/名称,但仍然没有验证属性.
那么有没有办法让我的 senario 工作 - 我不介意重写它,但我真的希望 FluentValidation 为我编写我的 html。
我的后备解决方案是让我自己的 Html 助手生成带有属性的正确 Html,但我认为这很糟糕,因为当对 FluentValidation、jquery 验证或两者之间的mvc链接。
jquery - 使用 AdditionalFields 对模型集合进行远程验证
当视图上只有一个模型实例时,远程验证工作正常。
问题是当我的视图处理模型集合时。这是我的模型:
在我看来,我有:
这是生成表单的很长的路,有人可以为我改进语法吗?
生成以下 html 表单:
尽管上面的 html 看起来相当不错(集合中的每个模型都有唯一的 id 和名称),但远程验证的附加字段存在问题:
在第二行触发远程验证时,第一行的 ID 会被提取。
asp.net-mvc-3 - 强制重新验证 mvc3 不显眼的远程验证
这是一个经典的登录流程。用户可以在“新用户”或“现有用户”之间进行选择。如果用户是新用户,则登录框中的名称应针对服务器进行验证,以查看用户名是否唯一,如果是现有用户,则将跳过此检查,因为我们希望用户名已经被使用(当然)。
我在视图模型上添加了一个 [Remote] 属性,并将新/退出用户的单选按钮添加为“附加字段”。这样,如果它是现有用户,远程验证将只返回 true,如果不是,则检查数据库,以查看是否使用了用户名。
这很好用,除非用户在输入用户名后决定更改单选按钮(新的/现有的)(并且远程验证已经运行)。由于远程验证仅在用户名更改时自动运行(即具有 [Remote] 属性的属性),因此仅更改单选按钮将不会再次运行。
所以我的问题是,我怎样才能强制远程验证再次运行?我通过在用户名输入字段上触发更改/焦点/模糊事件来尝试通常的黑客攻击,但未触发调用。我考虑在单选按钮上添加一个类似的 [Remote],但这会使两个看起来相同的错误消息变得复杂,放置在相同的绝对位置。
有没有办法触发重新验证?