Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
4206 Vizualizări

Hello,

I want to add validation for email fields and phone numbers, if the user enter text without @ validation error raise.



thank you in advance

Imagine profil
Abandonează
Cel mai bun răspuns

Hi,

You can do the validation according to the below code:

import re
from odoo.exceptions import ValidationError

@api.model
def create(self, vals):
    if vals.get('email'):
        match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', vals.get('email'))
        if match == None:
            raise ValidationError('Not a valid E-mail ID')

    if vals.get('number'):
        match = re.match('\\+{0,1}[0-9]{10,12}', vals.get('number'))
        if match == None:
            raise ValidationError('Invalid Phone Number')
    return super(MultiContacts, self).create(vals)

Regards

Imagine profil
Abandonează
Cel mai bun răspuns

Hello Jenam

you can implement constrains for your model as  i have done in res.partner model.

from odoo import api
from odoo.exceptions import UserError

class ResPartner(models.Model):
_inherit = "res.partner"

@api.constrains('email')
def _check_email_number(self):
for rec in self:
if rec.email and '@' not in rec.email:
raise UserError(_('Please Enter Correct Email'))

Thanks & Regards,



CandidRoot Solutions Pvt. Ltd.

Mobile: (+91) 8849036209
Email: info@candidroot.com
Skype: live:candidroot
Web: https://www.candidroot.com
Address: 1229-1230, Iconic Shyamal, Near Shyamal Cross Road, Ahmedabad, Gujarat



Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
2
dec. 23
14767
0
oct. 23
33
3
oct. 23
788
1
oct. 23
569
1
aug. 23
2492