OpenERP电子邮件验证 [英] OpenERP Email validation

查看:120
本文介绍了OpenERP电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请让我知道,当用户输入无效的电子邮件地址时,如何防止保存记录.现在,系统会按照设计显示有关无效电子邮件地址的警告消息,但还会保存带有无效电子邮件地址的记录.如何防止电子邮件地址无效的系统保存记录

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

从osv导入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()

查看xml

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

推荐答案

您需要修改create write和validate函数.希望您的validateemail方法正确.只要re.match为None,就会显示警告.

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 

这篇关于OpenERP电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆