Openerp中的客户端验证 [英] Client side validation in openerp
问题描述
我仍在学习Openerp,如果我问的很简单,请忍受. 我的问题是我需要验证两个代表start_time和end_time的字段.
I am still learning Openerp and please bear it if I asked something very simple. My issue is that I need to get validate two fields which represent start_time and end_time.
两个字段均位于char
both fields are in char
'start_time': fields.char('Start Time'),
'end_time': fields.char('End Time'),
我需要做的是,一旦用户输入了此开始时间和结束时间,我就需要检查该输入是否在24小时内以 hh:mm 模式进行.
What I need to do is , once the user input this start_time and end_time I need to check if that input is in 24hrs and in hh:mm pattern.
请足够友好地帮助我
推荐答案
您应该在Python代码中添加on_change函数,以检查start_time和end_time的格式是否正确.并且在您的xml中,您必须告诉您在字段更改时应调用该方法.
You should add an on_change function to your python code where you check if start_time and end_time is in the correct format. And in your xml you'll have to tell that the method should be called when the field changes.
XML
<field name="start_time" on_change="check_hour_format(start_time)"/>
<field name="end_time" on_change="check_hour_format(end_time)"/>
Python
结果应该类似于
def check_hour_format(self,cr,uid,ids,time_field,context=None):
if correct format
return {}
else:
warning = {'title' : _("Warning for this value!"),
'message': _("Field not in correct format!"),
}
return {'warning': warning}
此代码应可解决此问题
import time
def check_hour_format(self,cr,uid,ids,time_field,context=None):
try:
time.strptime(char_input, "%H:%M")
return {}
except ValueError:
warning = {'title' : _("Warning for this value!"),
'message': _("Field not in correct format!"),
}
return {'warning': warning}
在on_change方法中,您可以更改字段值
def on_change(self, cr, uid, ids, context=None):
# do something
return {'value': { 'field_name': newValue},
'warning': {'title': _("Warning"),
'message': _("warning message")
}
}
这篇关于Openerp中的客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!