Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

allow only numeric value fields.char

By
philip
on 8/24/14, 12:19 PM 3,691 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 

1

Med Said BARA

--Med Said BARA--
2664
| 5 5 7
Algeria
--Med Said BARA--
Med Said BARA
On 8/24/14, 2:34 PM

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/

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'

philip
on 8/24/14, 3:25 PM

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

philip
on 8/24/14, 3:47 PM
0

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 8/26/14, 4:43 AM

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

Thanks Sir Bole for your reply, I will try this

philip
on 8/26/14, 10:41 AM

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), }

philip
on 8/26/14, 11:43 AM
0

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 8/26/14, 4:59 AM

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.

Thank you Sir René Schuster will try booth solution

philip
on 8/26/14, 10:44 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 8/24/14, 12:19 PM
Seen: 3691 times
Last updated: 3/16/15, 8:10 AM