[英]Data is missing after using @validate with Schema
I'm working on validating my webapp, which is using Turbogears 2.3.3 and formencode 1.3 我正在验证使用Turbogears 2.3.3和formencode 1.3的Web应用程序
I want to validate a dynamic form that the user has created through a form creation process. 我想验证用户通过表单创建过程创建的动态表单。 I'm sending the form fields from the client to the server using json to help organize things.
我正在使用json将表单字段从客户端发送到服务器,以帮助组织事情。
Here is how I'm sending the data: 这是我发送数据的方式:
var dataToSend = JSON.stringify({
'num_of_copies': num_of_copies.val(),
'amountAnswers' : amountAnswers,
'yesNoAnswers' : yesNoAnswers,
'selectAnswers' : selectAnswers,
'comments':comments.val()
})
$.ajax({
type: 'POST',
url: siteProxy+'orders/saveOrderItem',
data: {'data':dataToSend},
dataType: "json",
success: function (data, textStatus) {
if (textStatus == "success") {
if (data.errors){
console.log(data.errors)
}
}
},
error: function (data, textStatus) {
alert('error');
}
})
On The server I want to validate the data and then do some stuff 在服务器上,我要验证数据,然后执行一些操作
@expose('json')
@validate(validators=orderItemSchema(),error_handler=simpleErrorHandler)
def saveOrderItem(self,**kw):
answers = json.loads(kw['data'])
...... do stuff ...
Without the validations, my code works. 没有验证,我的代码将起作用。
Here is my validation Schema: 这是我的验证架构:
class orderItemSchema(Schema):
def _convert_to_python(self, value_dict, state):
value_dict = json.loads(value_dict['data'])
super(orderItemSchema,self)._convert_to_python(value_dict, state)
num_of_copies = validators.Number(min=1)
comments = validators.UnicodeString()
amountAnswers = ForEach(AmountAnswerValidator())
yesNoAnswers = ForEach(YesNoAnswerValidator())
selectAnswers = ForEach(SelectAnswerValidator())
The validation works well. 验证效果很好。
My problem is this: after the validation, kw turns to none, and I can't do stuff in
我的问题是:验证后,kw变为无,并且我无法进行处理
def saveOrderItem(self,**kw):
I think the problem lies somewhere in this part of the code: 我认为问题出在代码的这一部分:
class orderItemSchema(Schema):
def _convert_to_python(self, value_dict, state):
value_dict = json.loads(value_dict['data'])
super(orderItemSchema,self)._convert_to_python(value_dict, state)
Thanks for the help 谢谢您的帮助
Probably orderItemSchema._convert_to_python
is missing return value. orderItemSchema._convert_to_python
可能缺少返回值。 Should be return super(orderItemSchema,self)._convert_to_python(value_dict, state)
or you will be returning None
as the converted value. 应该
return super(orderItemSchema,self)._convert_to_python(value_dict, state)
否则您将返回None
作为转换后的值。
If you are using a recent tg version I suggest you also have a look at @decode_params
decorator ( http://turbogears.readthedocs.org/en/latest/reference/classes.html#tg.decorators.decode_params ), it will extract the controller parameters from the json body and let validation flow as usual. 如果您使用的是最新的tg版本,建议您也查看
@decode_params
装饰器( http://turbogears.readthedocs.org/en/latest/reference/classes.html#tg.decorators.decode_params ),它将提取json主体中的控制器参数,然后像往常一样进行验证。 It will avoid the two json.load
in your code. 它将避免代码中的两个
json.load
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.