Search code examples
pythonxmlodoo

Selection field with widget="radio" not getting required effect applied with attrs in XML file in Odoo 12


I am trying to make a selection field with widget="radio" required using attrs in an XML file. The selection field doesn't get required with widget="radio" applied to it. but When I remove the radio widget, the selection field gets the required effect in form view upon creating new records.

This is the selection field where I appied required there:

<field name="installments_calculation" widget="radio" options="{'horizontal': true}" attrs="{'required': [('repayment_method', '=', 'salary deduction')]}"/>

And this is my repayment_method:

repayment_method = fields.Selection([('cash/bank', 'Cash/Bank'), ('salary deduction', 'Salary Deduction')])

I want the selection field gets required with applying the required attribute upon a condition in the XML file. Is this behavior is normal with selection fields with widget="radio" or I have done something wrong? If this is normal, how can I get the selection field required with widget="radio"?


Solution

  • Your code should work normally but if this problem is with the widget report a issue in Odoo Github :

    For now just use api.constrains to get the same behavior

     # remember to depend on both fields
     @api.constrains('installments_calculation','repayment_method')
     def check_installments_calculation(self):
        for rec in self:
            if not rec.installments_calculation and rec.repayment_method == 'salary deduction':
                raise exception.ValidationError(_('You message here'))