2014-02-26 157 views
1

請讓我知道,如何防止記錄在用戶輸入無效的電子郵件地址時得到保存。此時系統會顯示警告信息,表示按設計的無效電子郵件地址,但它也會保存帶有無效電子郵件地址的記錄。我們怎樣才能防止系統OSV進口OSV無效的電子郵件地址OpenERP電子郵件驗證

保存記錄

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)"/> 

enter image description here

回答

1

您需要修改創建寫入和驗證functions.I希望你validateemail方法是correct.Whenever的re.match是無,則警告將被顯示。

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 
+0

OmaL,剛剛取代你的代碼,並得到結果。 :-) – user3153567

+0

OmaL,我相信你也有這個解決方案..請幫助http://stackoverflow.com/questions/22015410/openerp-email-sent-confirmation-message-display – user3153567

1

Propably您可以覆蓋createwrite方法提出錯誤輸入的ValidateError錯誤。參考這裏: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() 
+0

嗨,PeterMmm - 我可以知道確切位置,我可以看到的創建和wrtite方法。 (對不起,我是OpenERP的新手 – user3153567

+0

它在第一個鏈接中,格式不正確,您必須向下滾動或搜索'create('。 – PeterMmm

+0

感謝您的快速更新,我想知道我在哪裏路徑可以在openerp(7.0版)應用程序中看到「創建」和「寫入」方法**代碼**。 – user3153567

0

這是更容易

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

你能解釋一下嗎?更多?謝謝 – Tessnim