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"?
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'))
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;
},
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With