我有一个整数字段' is_mobile'。用户进入手机的地方。我希望当用户输入错误的手机号码时。格式然后它显示警告消息"无效否。格式"没有正确的格式也无法保存。对于我应用的代码,但它不符合要求。
代码如下:
is_mobile = fields.Integer("Mobile")
@api.multi
@api.constrains('is_mobile')
def _check_phone_number(self):
for rec in self:
if rec.is_mobile and len(rec.is_mobile) != "^[0-9]{10}$" :
raise ValidationError(_("Wrong value enter"))
else:
return False
return {}
提前致谢
答案 0 :(得分:2)
@api.onchange('mobile')
def _onchange_mobile(self):
if self.mobile:
if re.match("^[0-9]\d{10}$", self.mobile) == None:
raise ValidationError("Enter valid 10 digits Mobile number")
您可以使用 onchange 。不要忘记导入重新
答案 1 :(得分:2)
如果您正在谈论验证,则无需在py中添加方法。只需在xml中添加 type =" tel" 即可。喜欢这个
<input type="tel" name="phone"/>
您可以参考基本代码,并了解我到底在说什么。