OpenERP Email validation

前端 未结 4 1956
萌比男神i
萌比男神i 2021-01-25 13:26

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

相关标签:
4条回答
  • 2021-01-25 13:43

    This is more easy

    from validate_email import validate_email
    is_valid = validate_email('email@is.valid')
    
    0 讨论(0)
  • 2021-01-25 13:46

    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 
    
    0 讨论(0)
  • 2021-01-25 13:49

    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
    
    0 讨论(0)
  • 2021-01-25 13:52

    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()
    
    0 讨论(0)
提交回复
热议问题