This question has been flagged
4 Replies
14186 Views

Hi All,

Is there anyone can figureout what we need to be done. Code is working when I entered char value the "raise (_('Invalid phone'),_('Please enter a valid phone'))" popup on my screen then if I hit the ok button and click save button and it will save, I want only numeric value to save. is there additional code can be added?

Thanks again for your reply

xml---

<field name="mobile" string="Phone" placeholder="434343454" on_change="onchange_mobile(mobile)"/>

python---

    def onchange_mobile(self, cr, uid, ids, mobile, context=None):
        if not mobile:
            return {}
        if not mobile.isdigit():
            raise osv.except_osv(_('Invalid phone'),_('Please enter a valid phone'))
        return {}

 

any help 

Avatar
Discard
Best Answer

Where the field is defined in your python code (_columns ....) ?

'mobile': fields.integer('Mobile', required =........, ..................., size= .....)

A ready solution: http://bazaar.launchpad.net/~aristobulo/web-addons/web_fields_masks/files/head:/web_fields_masks/

Avatar
Discard
Author

Thanks Sir, in my python I have the following: _columns = { 'mobile': fields.char('Mobile Phone', size=11), } I did already change from fields.char to fields.integer but I got the following error: if not mobile.isdigit(): AttributeError: 'int' object has no attribute 'isdigit' 2014-08-24 19:21:13,526 11482 ERROR ABS openerp.netsvc: 'int' object has no attribute 'isdigit' Traceback (most recent call last): File "/home/jp/ws/openerp/server/openerp/netsvc.py", line 296, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/home/jp/ws/openerp/server/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, *params) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 190, in execute_kw return self.execute(db, uid, obj, method, *args, **kw or {}) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 132, in wrapper return f(self, dbname, *args, **kwargs) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 199, in execute res = self.execute_cr(cr, uid, obj, method, *args, **kw) File "/home/jp/ws/openerp/server/openerp/osv/osv.py", line 187, in execute_cr return getattr(object, method)(cr, uid, *args, **kw) File "/home/jp/ws/openerp/my_addons/abs_hr_additional_info/abs_hr_additional_info.py", line 11, in onchange_mobile if not mobile.isdigit(): AttributeError: 'int' object has no attribute 'isdigit'

Author

Sir Thank you so much and really appreciate your help. Here's what I did replace my to many thanks again

Best Answer

Us can Try this one:

@api.constrains('phone_number')
def _verify_phone_number(self):
for rec in self:
if rec.phone_number and not rec.phone_number.isdigit():
raise ValidationError(_("The Phone Number must be a sequence of digits."))


Avatar
Discard
Best Answer

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

Avatar
Discard
Author

Thanks Sir Bole for your reply, I will try this

Author

Hi Sir Bole, Tried to test the above code I Got the Following errors: Client Traceback (most recent call last): File "/home/philip/ws/openerp/web/addons/web/http.py", line 204, in dispatch response["result"] = method(self, **self.params) File "/home/philip/ws/openerp/web/addons/web/controllers/main.py", line 1125, in call_kw return self._call_kw(req, model, method, args, kwargs) File "/home/philip/ws/openerp/web/addons/web/controllers/main.py", line 1117, in _call_kw return getattr(req.session.model(model), method)(*args, **kwargs) File "/home/philip/ws/openerp/web/addons/web/session.py", line 42, in proxy result = self.proxy.execute_kw(self.session._db, self.session._uid, self.session._password, self.model, method, args, kw) File "/home/philip/ws/openerp/web/addons/web/session.py", line 30, in proxy_method result = self.session.send(self.service_name, method, *args) File "/home/philip/ws/openerp/web/addons/web/session.py", line 103, in send raise xmlrpclib.Fault(openerp.tools.ustr(e), formatted_info) Server Traceback (most recent call last): File "/home/philip/ws/openerp/web/addons/web/session.py", line 89, in send return openerp.netsvc.dispatch_rpc(service_name, method, args) File "/home/philip/ws/openerp/server/openerp/netsvc.py", line 296, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/home/philip/ws/openerp/server/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, *params) File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 190, in execute_kw return self.execute(db, uid, obj, method, *args, **kw or {}) File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 132, in wrapper return f(self, dbname, *args, **kwargs) File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 199, in execute res = self.execute_cr(cr, uid, obj, method, *args, **kw) File "/home/philip/ws/openerp/server/openerp/osv/osv.py", line 187, in execute_cr return getattr(object, method)(cr, uid, *args, **kw) File "/home/philip/ws/openerp/my_addons/for_development/philcode_test.py", line 16, in onchange_mobile res['value']['mobile'] = False # just erase the value entered KeyError: 'value' Here the XML and python file XML file----- PYTHON FIle----- import time from osv import osv, fields from openerp import tools class philcode_test(osv.Model): _name = "philcode.test" 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 _columns = { 'a': fields.char('A', size=12), 'b': fields.char('B', size=12), 'mobile': fields.char('Mobile Phone', size=11), }

Best Answer

The on_change() method can only return values, domains or warnings. If you don't want a record with a non numeric phone number to be safed, then either follow Boles hint and override the write() method, or use the built in functionality of constraints!

def _check_mobile(self, cr, uid, ids, context=None):
     for obj in self.browse(cr, uid, ids, context=context):
          if not obj.mobile.isdigit():
              return False
     return True

_constraints = [(_check_mobile, _('Error: Wrong Phone Number Format!'), [mobile'])]

Add this code to your .py file and leave the field as char.

The constraints will be checked whenever a record is saved (creation/modification).

 

Regards.

Avatar
Discard
Author

Thank you Sir René Schuster will try booth solution