Search code examples
python-3.xodoo

a warning field for a form in odoo needed when opening form


I am trying to add warning field to form that if there is a message in the filed the message will popup every time you open the form:

message_warning = fields.Char(string="Warning", )

And I want it if its empty nothing happened and if the user added text then every time the form opened the message inside the form popup. I searched the web and found this function that suppose to do the task but nothing happened no pop up:

@api.model    
def form_view_get(self, access_uid, view_id=None, view_type='form', context=None, options=None):
    res = super(Model_name, self).form_view_get(access_uid, view_id, view_type, context, options)        
    if context.get('create') or context.get('edit'):  # Check if opening for create or edit            
        record_id = context.get('id')  # Get record ID if available            
    if record_id and self.env['model.name'].browse(record_id).required_field:                # Raise warning without preventing form opening                
        raise UserError("message!")

I did try the above and no message appears when opening the form and no error message just nothing happened.


Solution

  • You can use a div at the top of the form view to show the warning message

    Example:

    <div class="alert alert-warning" role="alert" invisible="not message_warning">
        <field name="message_warning" readonly="1"/>
    </div>