敲除验证
我有一个 asp.net mvc3 项目,我在其中使用淘汰赛绑定对表进行批量编辑。我想在保存数据时进行必需验证和数字验证等验证。有没有更简单的方法来进行淘汰验证。 PS:我没有使用表格。
I have asp.net mvc3 project where I do a bulk edit on a table with knockout binding. I want to do validations like required and number validations while saving data. Is there any easier way to do knock out validations.
PS: I am not using forms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 Knockout-Validation ,它干净地设置并使用 淘汰文档。下:实时示例 1:强制输入为数字
您可以在 Fiddle 中实时查看
更新: fiddle 已更新为使用 cloudfare CDN url 来使用最新的 KO 2.0.3 和 ko.validation 1.0.2
要设置 ko.validation:
要设置验证规则,请使用扩展器。例如:
Have a look at Knockout-Validation which cleanly setups and uses what's described in the knockout documentation. Under: Live Example 1: Forcing input to be numeric
You can see it live in Fiddle
UPDATE: the fiddle has been updated to use the latest KO 2.0.3 and ko.validation 1.0.2 using the cloudfare CDN urls
To setup ko.validation:
To setup validation rules, use extenders. For instance:
如果您不想使用 KnockoutValidation 库,您可以编写自己的库。以下是必填字段的示例。
添加一个带有所有 KO 扩展或扩展器的 javascript 类,并添加以下内容:
然后在您的 viewModel 中通过以下方式扩展您的可观察对象:
网上有很多这种验证方式的示例。
If you don't want to use the KnockoutValidation library you can write your own. Here's an example for a Mandatory field.
Add a javascript class with all you KO extensions or extenders, and add the following:
Then in your viewModel extend you observable by:
There are a number of examples online for this style of validation.
Knockout.js 验证很方便,但不够健壮。您始终必须创建服务器端验证副本。在您的情况下(当您使用 Knockout.js 时),您将 JSON 数据异步发送到服务器并返回,因此您可以让用户认为他看到了客户端验证,但实际上这将是异步服务器端验证。
看一下这里的示例 upida.cloudapp.net :8080/org.upida.example.knockout/order/create?clientId=1
这是“创建订单”链接。尝试点击“保存”,然后玩产品。
这个例子是使用codeplex的upida库(这个库有spring mvc版本和asp.net mvc)完成的。
Knockout.js validation is handy but it is not robust. You always have to create server side validation replica. In your case (as you use knockout.js) you are sending JSON data to server and back asynchronously, so you can make user think that he sees client side validation, but in fact it would be asynchronous server side validation.
Take a look at example here upida.cloudapp.net:8080/org.upida.example.knockout/order/create?clientId=1
This is a "Create Order" link. Try to click "save", and play with products.
This example is done using upida library (there are spring mvc version and asp.net mvc of this library) from codeplex.