Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odoo - prevent button from closing wizard

I have a transient model that serves as a dialog. In my form view I have a button like this:

<footer states="partnerId">
   <button name="check_tax_id" string="Tovább" type="object"/>
</footer>

The button invokes this function (I can confirm it actually invokes):

@api.one
    def check_tax_id(self, context=None):
        self.state = "partnerDetails"

        return None;

My problem is that the dialog window is closed immediately once I click this button! What am I doing wrong?

like image 996
Aron Lorincz Avatar asked Aug 12 '15 10:08

Aron Lorincz


1 Answers

yesterday I bumped on this same issue. I needed to show a button to do something without submitting the whole wizaard. I worked around it by not using a button at all. It's pretty simple and effective. What you need:

  1. a boolean flag in your wizard model
  2. an onchange attached to the flag (that replaces you sumbmit function)
  3. replace the button in the view w/ the flag w/ invisible="1" and a label to be styled as a button

Here's the code:

source_it = fields.Boolean(string='Source')
[...]
def action_source(self):
    # do stuff

@api.onchange('source_it')
def onchange_source_it(self):
    if self.env.context.get('sourcing_now') or not self.source_it:
        return
    self.action_source()
[...]
<label for="source_it" class="pull-left btn btn-success" />
<field name="source_it" invisible="1" />

The trick works because when a label has for attribute is going to act like the checkbox itself, so if you click on the label you are actually switching the checkbox.

like image 52
simahawk Avatar answered Sep 19 '22 14:09

simahawk