Shridhar Ivani
Shridhar Ivani

Reputation: 467

How add validation message in hr module in odoo?

In hr module ,hr.employee class has following fields so for this fields i wanted have valid phone number with 10 numbers only if i enter more than 10 numbers it should show message like enter valid num

'work_phone': fields.char('Work Phone', readonly=False),
'mobile_phone': fields.char('Work Mobile', readonly=False),

Upvotes: 0

Views: 1112

Answers (3)

Jainik Patel
Jainik Patel

Reputation: 2314

If you want to use onchange method to get warning on invalid chars in field, then try this:

 def onchange_mobile(self, cr, uid, ids, mobile, context=None):
    res = {}
    if not mobile:
         return res

    if not mobile.isdigit():
        # raise osv.except_osv(_('Invalid phone'),_('Please enter a valid phone'))
        res['warning'] = "Phone number %s is invalid, please use only digits!" % mobile
        res['value']['mobile'] = False   # just erase the value entered
    return res

Or, you can override thw write method of your working class and raise error if field 'mobile' is not numeric... like

def write(self, cr, uid, ids, vals, context=None):
    if 'mobile' in vals.keys() and not vals['mobile'].isdigit():
        raise osv.except_osv(_('Invalid phone'),_('Please enter a valid phone'))
    return super(your_class, self).write(cr, uid, ids, vals, context=context)

hope it helps

Upvotes: 1

Rutul Raval
Rutul Raval

Reputation: 313

import re

def is_phone(self, cr, uid, ids, context=None):
    record = self.browse(cr, uid, ids)
    pattern ="^[0-9]{10}$"
    for data in record:
        if re.match(pattern, data.phone):
            return True
        else:
            return False
    return {}

_constraints = [(is_phone, 'Error: Invalid phone', ['phone']), ]

This way you can match the phone number with regular expression.

View side

<fields name="phone" onchange="is_phone()"> 

Upvotes: 2

Prashant
Prashant

Reputation: 704

def on_change('number'):
    Check here length of number greater than 10 or not if greater, then rais or validate error

view side
<Fields name="name" onchange="on_change('number')"> 

or

use constraints the batter

or check in create method

Upvotes: 0

Related Questions