Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

allow only numeric value fields.char

Naroči se

Get notified when there's activity on this post

This question has been flagged
4 Odgovori
15960 Prikazi
Avatar
philip

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 

0
Avatar
Opusti
Avatar
Med Said BARA
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/

1
Avatar
Opusti
philip
Avtor

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
Avtor

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

Avatar
Muhammad Rizwan
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."))


0
Avatar
Opusti
Avatar
Bole
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

0
Avatar
Opusti
philip
Avtor

Thanks Sir Bole for your reply, I will try this

philip
Avtor

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

Avatar
René Schuster
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.

0
Avatar
Opusti
philip
Avtor

Thank you Sir René Schuster will try booth solution

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Prijavi
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now