Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

xml

odoo

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"?

like image 595
Ibrahim Rahimi Avatar asked Jun 02 '19 05:06

Ibrahim Rahimi


2 Answers

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'))
like image 76
Charif DZ Avatar answered Sep 28 '22 08:09

Charif DZ


I came up with a fix for the client side which is the approach that I was looking for:

odoo.define('mymodule.web.radio.required', function(require){
  var relational_fields = require('web.relational_fields');

  relational_fields.FieldRadio.include({
      isSet: function () {
          return (this.mode === 'edit')? ($(this.$el).find("[checked='true']")).length > 0: this.value;
      },
  });
});
like image 21
Yusnel Rojas García Avatar answered Sep 28 '22 06:09

Yusnel Rojas García