淘汰赛验证和 Qtip
我目前使用 Jquery Validation 和 Qtip 一起处理实际验证,并使用验证选项的 errorPlacement 组件在验证错误时使用漂亮的工具提示样式通知来处理实际验证并在屏幕上显示信息。
目前,每个 viewModel 都有自己的自定义方法来设置和启动验证和回调,但是我试图寻找一种更好的方法来执行此操作,无论是添加自定义绑定来通过数据绑定设置我的验证规则还是另一种方法,但仍然产生相同的结果(即,当发生验证错误时触发 errorPlacement 并告诉 Qtip 显示给定元素的错误)。
现在,在我开始自己制作一个之前,我刚刚在网上查了一下,发现 Knockout Validation,我最初认为是一个好主意,我可以将我的验证逻辑直接应用于我的 viewModel 中的数据,然后只需找到某种回调来让 Qtip 启动,但是似乎没有我可以找到的回调记录。该库似乎为验证方面做了我想做的一切,只是不为显示方面做事。我查看了源代码和示例,但除了 ko.validation.group(viewModel) 之外看不到任何其他内容,这会给我一个包含错误的可观察值,但我不确定是否可以像我一样使用它期待。
以下是我当前验证如何发生的示例:
/*globals $ ko */
function SomeViewModel() {
this.SetupValidation = function () {
var formValidationOptions = {
submitHandler: self.DoSomethingWhenValid,
success: $.noop,
errorPlacement: function (error, element) {
if (!error.is(':empty'))
{ qtip.DoSomethingToDisplayValidationErrorForElement(element, error); }
else
{ qtip.DoSomethingToHideValidationErrorForElement(element); }
}
};
$(someForm).validate(formValidationOptions);
this.SetupValidationRules();
};
this.SetupValidationRules = function() {
$(someFormElement1).rules("add", { required: true, minlength: 6, maxlength: 20, alphaNumeric: true });
$(someFormElement2).rules("add", { required: true, minlength: 6, maxlength: 20 });
$(someFormElement3).rules("add", { required: true, email: true, });
};
}
我目前确信可以通过添加自定义绑定来消除对验证规则方法的需求,以便我可以在数据绑定中设置验证,但是如果可能的话我想使用与现有的 Knockout-Validation 绑定相同的回调方法。
I currently use Jquery Validation and Qtip together to deal with the actual validation and displaying of information to the screen using the nice tooltip style notifications upon validation errors using the errorPlacement component of the validation options.
Currently each viewModel has its own custom method for setting up and kicking off the validation and callbacks, however I was trying to look at a nicer way of doing this, be it adding a custom binding to setup my validation rules via the data-bindings or an alternative way, but still yielding the same results (i.e the errorPlacement is triggered when a validation error occurs and tells Qtip to display the error for the given element).
Now before I started making one myself I just checked online and found Knockout Validation, which I initially thought was a great idea, I could apply my validation logic directly to the data within my viewModel and then just find some sort of callback to get Qtip to kick in, however it seems there is no callback that I can find documented. The library seems to do everything I want for the validation side of things, just not for the displaying side of things. I looked through the source code and examples but couldn't see anything other than ko.validation.group(viewModel) which would give me an observable containing the errors, but I am not sure if I could use this the same way as I was expecting.
Here is an example of how my current validation happens:
/*globals $ ko */
function SomeViewModel() {
this.SetupValidation = function () {
var formValidationOptions = {
submitHandler: self.DoSomethingWhenValid,
success: $.noop,
errorPlacement: function (error, element) {
if (!error.is(':empty'))
{ qtip.DoSomethingToDisplayValidationErrorForElement(element, error); }
else
{ qtip.DoSomethingToHideValidationErrorForElement(element); }
}
};
$(someForm).validate(formValidationOptions);
this.SetupValidationRules();
};
this.SetupValidationRules = function() {
$(someFormElement1).rules("add", { required: true, minlength: 6, maxlength: 20, alphaNumeric: true });
$(someFormElement2).rules("add", { required: true, minlength: 6, maxlength: 20 });
$(someFormElement3).rules("add", { required: true, email: true, });
};
}
I currently am sure I can remove the need for the validation rules method by adding a custom binding so I can set the validation in the data-bind, however if possible I would like to use the same sort of callback approach with the existing Knockout-Validation binding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有专门使用过淘汰验证,但我过去写过类似的东西。快速浏览一下源代码就会发现,每个扩展可观察值都有一个子可观察值 isValid。这可用于使用传统的剔除可见绑定来隐藏标记中的显示消息。
要使 QTip 正常工作,自定义绑定可以订阅此 isValid 属性并执行必要的初始化以在触发时显示/隐藏 QTip。
编辑
这是一个入门示例
http://jsfiddle.net/madcapnmckay/ hfcj7/
HTML:
JS:
I haven't used Knockout-Validation specifically but I have written something similar in the past. A quick glance at the source shows that each extended observable gets a sub-observable isValid. This could be used to hide show messages in your markup using conventional knockout visible bindings.
To get QTip to work a custom binding could subscribe to this isValid property and perform the necessary initialization to show/hide QTip when triggered.
EDIT
Here is an example to get you started
http://jsfiddle.net/madcapnmckay/hfcj7/
HTML:
JS:
我一直在编辑 madcapnmckay 的帖子,但差异已经变得足够大,我认为需要一个新的答案。
它很大程度上基于 madcapnmckay 的帖子,但它修复了 MorganTiley 指出的错误。仅当用户修改了可观察值时,原始版本才有效。如果他们没有,那么代码永远不会被触发。因此,我对其进行了修改,以便在创建工具提示代码时以及更改时触发工具提示代码。
一个缺点是,在运行淘汰验证之前,工具提示将在悬停时显示(例如,您对某个字段进行了“必需”验证,在按下提交之前,工具提示将显示该字段是必填字段,但该字段不会以粉红色突出显示)。但是,一旦更改字段,如果字段有效,工具提示就会消失。
我的应用程序没有使用 qtip,而是使用 Twitter Bootstrap Tooltip,因此这里也是其代码。
I had been editing madcapnmckay's post, but the differences have become significant enough that I think a new answer is needed.
It is heavily based off of madcapnmckay's post, but it fixes a bug pointed out by MorganTiley. The original only works if the user has modified the observable. If they haven't then the code never gets fired. So, I've modified it so that it fires the tooltip code when it gets created, in addition to when it changes.
The one downside is that the tooltip will display on hover before knockout validation has been run (example, you have a "required" validation on a field, before you press submit a tooltip will display saying the field is required, but the field will not highlight in pink). Once you change the field however, the tooltip will disappear if the field is valid.
My app was not using qtip, but rather Twitter Bootstrap Tooltip, so here is the code for that as well.