2016-04-26 182 views
0

這是參考我關於Client side validation in openerp客戶端驗證的OpenERP

我需要進一步澄清,並請善待足以幫助我這個問題。

我需要知道的是,即使我檢查了值並在結果爲false時得到錯誤消息;一旦錯誤消息關閉,我仍然可以保存錯誤的類型值。

請幫我避免,直到你輸入正確的值,你不能繼續。

檢查領域將是,

'start_time': fields.char('Start Time'), 

和功能將是

def check_start_time(self,cr,uid,ids,start_time_check,context=None): 
    starting_time=start_time_check 
    try: 
     time.strptime(starting_time, "%H:%M") 
     return True 
    except ValueError: 
     raise osv.except_osv(('Error'), ('Start time not in hh:mm format (Eg: 08:30)')) 
+0

可以調用'check_start_time'功能更多的時間在'write'或'然後create'方法不能保存,直到正確的值被輸入。 – SDBot

+0

類中已經有一個創建方法,我可以再次調用另一個創建方法嗎?非常感謝你提供快速支持btw ... –

+0

你不能只編輯現有的'create'方法嗎? – SDBot

回答

1

您可以覆蓋在模型的默認方法。如果你有一個,你不需要一個新的創建方法。只需插入你的領域條件。
(在你的情況,你必須同時重寫的創建和寫入,因爲創建運行時用戶要創建一個新的記錄,寫運行時用戶要更新的記錄。)

另一個解決,當你使用約束。

的Python

def create(self, cr, uid, vals, context=None): 
    try: 
     time.strptime(starting_time, "%H:%M") 
    except ValueError: 
     raise osv.except_osv(('Error'), ('Start time not in hh:mm format (Eg: 08:30)')) 
    # do something 
    return super(ModelName, self).create(cr, uid, vals, context) 

def write(self, cr, uid, vals, context=None): 
    try: 
     time.strptime(starting_time, "%H:%M") 
    except ValueError: 
     raise osv.except_osv(('Error'), ('Start time not in hh:mm format (Eg: 08:30)')) 
    # do something 
    return super(ModelName, self).write(cr, uid, vals, context) 



def _check_startTime(cr, uid, ids): 
    try: 
     time.strptime(starting_time, "%H:%M") 
     return True 
    except ValueError: 
     return False 

_constraints = [(_check_startTime, 'Invalid format!', ['start_time'])] 

Here is a very useful cheat sheet.