使用 jQuery 和 FormEncode 无需重复即可验证表单

发布于 2024-09-25 00:28:53 字数 400 浏览 0 评论 0原文

我正在开发一个基于 Pylons 的网络应用程序。因为我很理智,所以我使用 jQuery(和插件)而不是编写原始 JavaScript。我还使用 FormEncode 来验证我的应用程序的表单(尤其是新用户注册)。 FormEncode 非常适合在提交表单后对其进行验证。当 JavaScript 可用时,jQuery 可以在提交表单之前很好地验证表单。

我很贪婪:我想使用两种验证 - 而且我不想重复自己。如果有两组验证规则,则保持它们同步会产生额外的工作负载。

如何使用 jQuery 访问 FormEncode 验证规则,以便 jQuery 和 FormEncode 都基于相同的规则调查表单数据,而不必将规则写下来两次?

I'm working on a Pylons-based web app. Because I am sane, I am using jQuery (and plugins) instead of writing raw JavaScript. I am also using FormEncode to validate forms for my app (especially new user registration). FormEncode is great for validating forms after they're submitted. jQuery, when JavaScript is available, validates forms quite well before they're submitted.

I'm greedy: I want to use both kinds of validation - and I don't want to repeat myself. If there are two sets of validation rules, there's an extra workload generated by keeping them in sync.

How can I use jQuery to access my FormEncode validation rules, so that both jQuery and FormEncode investigate form data based on the same rules without having to write the rules down twice ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-10-02 00:28:53

我当前的解决方案是在控制器中使用 FormEncode 规则,并为控制器提供一种响应提交的完整表单的方法,以及另一种响应 AJAX 请求的方法,并再次验证这两种方法是否具有相同的 FormEncode 规则。这意味着我可以让 jQuery 向控制器的 AJAX-y 方法发出请求,并让常规表单提交转到控制器的正常表单提交方法,并且在这两种情况下都将应用同一组表单验证规则。

My current solution is to have the FormEncode rules in the controller, and to give the controller one method that responds to a complete form being submitted, and another method that responds to AJAX requests, and validate both methods again the same FormEncode rules. That means that I can have jQuery make requests to the controller's AJAX-y method, and have regular form submissions go to the controller's normal form-submission method, and in both cases the same set of form-validation rules will be applied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文