问题
In Odoo 8, I am using a custom module which checks that VAT is unique, using @api.constrains('vat', 'parent_id', 'company_id'), and raising a warning when vat already exists.
But in the website purchase checkout form, I am making the customers enter the VAT. It happens that when a repeated VAT is entered I get a 500 internal server error, since the website does not provide a way to raise the warning.
How should I implement a warning, a pop-up or similar in order to avoid those internal server errors?
回答1:
You can use a JSON-RPC call to backend when submit button is clicked, before sending form data.
Create a new controller in python for validating a VAT:
@http.route(['/vat/validator'], type='json', auth="public", website=True) def vat_validator(self, vat): # Your validation code here return 'OK'
Create a JS method for calling validator:
(function() { 'use strict'; function vat_validator(vat) { openerp.jsonRpc('/vat/validator', 'call', {'vat': vat}).then(function(result) { // Your JS code here for checking backend validator result }) } })();
Set a form validation JS method like this: http://www.w3schools.com/js/js_validation.asp
来源:https://stackoverflow.com/questions/34069000/how-can-i-raise-a-warning-message-in-odoo-website