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 a
This is more easy
from validate_email import validate_email
is_valid = validate_email('email@is.valid')
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
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
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()