user3153567
user3153567

Reputation: 157

OpenERP Email validation

Please let me know , how to prevent record getting saved when user enters invalid email address. Right now system displays warning message for invalid email address which is as per design but it also saves the record with invalid email address. How can we prevent system saving record with invalid email address

from osv import osv

 import smtplib
 import re

  from osv import fields

 class latest_base(osv.osv):
    _inherit = ['mail.thread']
    _name='latest.base'
    _columns={
        'name':fields.char('Name'),

        'image': fields.binary("Image", help="Select image here"),

        'state': fields.selection([
                ('new','New'),
                ('starts','Starts'),
                ('progress','Progress'),
                ('won','Won'),
                ('lost','Lost'), ('tied','Tied')], 'Stage',readonly=True),
        'email':fields.char('Email'),
        }

_sql_constraints = [('unique_name', 'unique(name)', 'Sorry ! A record with the same name already exists.')]


def mymod_new(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'new' })
    return True

def mymod_starts(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'starts' })
    return True

def mymod_progress(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'progress' })
    return True

def mymod_won(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'won' })
    return True

def mymod_lost(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'lost' })
    return True
def mymod_tied(self, cr, uid, ids):
    self.write(cr, uid, ids, { 'state' : 'tied' })
    return True


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

 # create method is overridden here

         def create(self, cr, uid,values,context=None):
              if not self.ValidateEmail(cr,uid,[],values['email']):
                   raise ValidateError()
              else:
                   res = super(latest_base,self).create(cr,uid,values,context=context)
                   return res


# write method is overridden here

          def write(self, cr, uid, ids, values, context=None):
               if not self.ValidateEmail(cr,uid,ids,values['email']):
                   raise ValidateError()
                else:  
                   res = super(latest_base, self).write(cr, uid, ids, values, context=context)
                   return res 

latest_base()

view xml

  <field name="email" on_change="ValidateEmail(email)"/>

enter image description here

Upvotes: 1

Views: 1552

Answers (4)

EM = (r"[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$")

def emailvalidation(email):

if email:
    EMAIL_REGEX = re.compile(EM)
    if not EMAIL_REGEX.match(email):
        raise ValidationError(_('''This seems not to be valid email.
        Please enter email in correct format!'''))
    else:
        return True

Upvotes: 0

endika_iglesias
endika_iglesias

Reputation: 111

This is more easy

from validate_email import validate_email
is_valid = validate_email('[email protected]')

Upvotes: 0

OmaL
OmaL

Reputation: 5044

You need to modify your create write and validate functions.I hope your validateemail method is correct.Whenever the re.match is None, then warning will be showed.

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

def create(self, cr, uid,values,context=None):
    if 'email' in values:
        self.ValidateEmail(cr,uid,[],values['email'])
    res = super(latest_base,self).create(cr,uid,values,context=context)
    return res

def write(self, cr, uid, ids, values, context=None):
    if 'email' in values:
        self.ValidateEmail(cr,uid,ids,values['email'])
    res = super(latest_base, self).write(cr, uid, ids, values, context=context)
    return res 

Upvotes: 1

PeterMmm
PeterMmm

Reputation: 24640

Propably you can override the create and write method to raise a ValidateError error for wrong input. References here: https://doc.openerp.com/trunk/server/api_models/

class latest_base(osv.osv):

   def create(self, cr, uid, values, context=None):
        if not self.ValidateEmail(cr,uid,[],values['email']):
            raise ValidateError()

Upvotes: 1

Related Questions