Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
3922 Weergaven

Hi, I have a scenario where I need to validate a email address whether it is valid or not. For validating I have used the following code.

hr_view.xml:

<field name="work_email" widget="email" on_change="onchange_work_email(work_email)"/>

hr.py

def onchange_work_email(self, cr, uid, ids, work_email): if re.match("^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$", work_email) != None: return True else: raise osv.except_osv(_('Invalid Email'), _('Please enter a valid email address'))

But I got No handler found error. I made changes in only above two files, Did i need to import regular expressions ? what is the error ? Any appreciate suggestions

Avatar
Annuleer
Beste antwoord

hii,


here your py updated code 
from odoo import models, fields, api

from odoo.exceptions import ValidationError

import re


class HrEmployee(models.Model):

    _inherit = 'hr.employee'


    work_email = fields.Char(string="Work Email")


    @api.onchange('work_email')

    def _onchange_work_email(self):

        if self.work_email:

            email = self.work_email.strip()

            pattern = r'^.+@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,}$'

            if not re.match(pattern, email):

                raise ValidationError("Please enter a valid email address.")


XML (View) – Do NOT add on_change in XML

Just define the field like normal:

<field name="work_email"/>

Odoo will trigger the @api.onchange automatically when the field changes in the UI.

i hope it is use full

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
sep. 19
10776
3
dec. 23
45959
0
mrt. 15
3795
0
mrt. 15
3910
1
dec. 22
3651