在网页odoo 12上显示电话号码格式的验证错误

时间:2019-09-24 10:21:25

标签: validation controller odoo odoo-12

我正在为用户注册过程。注册前我从用户那里拿电话的地方。而且我想在电话号码格式错误时显示验证结果。

我的控制器文件中的电话号码格式代码如下:

def passwordless_signup(self, values, qcontext):
    .
    .
    .
    if values['phone']:
        phone_fields = self._get_phone_fields_to_validate()
        for phone_field in phone_fields:
            number = values['phone']
            fmt_number = request.env['res.partner'].phone_format(number)
            request.params.update({phone_field: number})
    .
    .
    .
    return request.render("auth_signup.reset_password", qcontext)

在Ubuntu终端上,我遇到以下错误,我想将其显示为对用户的验证:

File "/opt/odoo12/odoo/addons/phone_validation/tools/phone_validation.py", line 25, in phone_parse
    raise UserError(_('Invalid number %s: probably incorrect prefix') % number)
odoo.exceptions.UserError: ('Invalid number 4545545: probably incorrect prefix', '')

预先感谢

2 个答案:

答案 0 :(得分:0)

您必须捕获异常,然后将带有错误消息的error键添加为qcontext字典中的值,以将其呈现为提供给前端的警告。

if values['phone']:
    phone_fields = self._get_phone_fields_to_validate()
    for phone_field in phone_fields:
        number = values['phone']
        try:
            fmt_number = request.env['res.partner'].phone_format(number)
            request.params.update({phone_field: number})
        except Exception as e:
            qcontext['error'] = str(e)

答案 1 :(得分:0)

在phone_validation.py中添加:from odoo import exceptions,在_get_phone_fields_to_validate方法中添加

if not {is_valid_phone}: raise exceptions.ValidationError('Invalid number %s: probably incorrect prefix' % {your_phone_value}) else: return {your_phone_value}