如何在客户端和服务器端上使用验证规则?

时间:2022-01-10 16:11:07

I'm using jQuery validation system for client-side validation. The backend works with django. jQuery use an interesting set of rules in JSON format.

我正在使用jQuery验证系统进行客户端验证。后端与django一起工作。jQuery使用了一组有趣的JSON格式规则。

Does exists something to use the same rules on django side or I need to code it myself?

在django上是否存在使用相同规则的东西,或者我需要自己编写它?

3 个解决方案

#1


2  

No, no such thing exists.

不,不存在这样的东西。

Yes, you need to code it yourself.

是的,你需要自己编码。

However, I imagine you could possibly create widgets which are able to deliver generic validation js routines based on the modelFields. Such as "This should be chars not more than max_length". However they could not trivially generate client-side code to validate any custom validation written in python, nor even something like "This must be the username of an existing user". But if you take this trouble to build widgets which do basic validation based on the modelFields please contribute them to open source :-D

但是,我认为您可以创建能够基于modelfield交付通用验证js例程的小部件。例如“这应该是chars不超过max_length”。但是,他们不能简单地生成客户端代码来验证用python编写的任何自定义验证,甚至不能生成“这必须是一个现有用户的用户名”之类的代码。但是,如果您在构建基于modelFields进行基本验证的小部件时遇到这种麻烦,请将它们贡献给开放源码:-D

#2


2  

This article describes some level of integration: http://streamhacker.com/2010/03/08/jquery-validation-django-forms/

本文描述了一些集成级别:http://streamhacker.com/2010/03/08/jquery- validjango -forms/

It doesn't look like it actually generates the clientside metadata based on the serverside metadata, but at least puts all rules in one place. May be a good starting point for generating those rules based on the existing Django rules.

看起来它并不是基于服务器端元数据生成客户端元数据,但至少将所有规则放在一个地方。可能是基于现有的Django规则生成这些规则的一个好起点。

#3


0  

I haven't used it myself, but there is the django-ajax-forms project.

我自己没有使用过,但是有django- ajaxform项目。

#1


2  

No, no such thing exists.

不,不存在这样的东西。

Yes, you need to code it yourself.

是的,你需要自己编码。

However, I imagine you could possibly create widgets which are able to deliver generic validation js routines based on the modelFields. Such as "This should be chars not more than max_length". However they could not trivially generate client-side code to validate any custom validation written in python, nor even something like "This must be the username of an existing user". But if you take this trouble to build widgets which do basic validation based on the modelFields please contribute them to open source :-D

但是,我认为您可以创建能够基于modelfield交付通用验证js例程的小部件。例如“这应该是chars不超过max_length”。但是,他们不能简单地生成客户端代码来验证用python编写的任何自定义验证,甚至不能生成“这必须是一个现有用户的用户名”之类的代码。但是,如果您在构建基于modelFields进行基本验证的小部件时遇到这种麻烦,请将它们贡献给开放源码:-D

#2


2  

This article describes some level of integration: http://streamhacker.com/2010/03/08/jquery-validation-django-forms/

本文描述了一些集成级别:http://streamhacker.com/2010/03/08/jquery- validjango -forms/

It doesn't look like it actually generates the clientside metadata based on the serverside metadata, but at least puts all rules in one place. May be a good starting point for generating those rules based on the existing Django rules.

看起来它并不是基于服务器端元数据生成客户端元数据,但至少将所有规则放在一个地方。可能是基于现有的Django规则生成这些规则的一个好起点。

#3


0  

I haven't used it myself, but there is the django-ajax-forms project.

我自己没有使用过,但是有django- ajaxform项目。