Skip to Content
Menu
This question has been flagged
2 Replies
746 Views

I am trying to hide a field based on an option selected from a drop down.

In my py file I have:


hide = fields.Boolean(string= 'Hide' , compute= "_compute_hide" )

@api.onchange( 'math_type_cfg_id' )

def _onchange_math_type_cfg_id(self):

    if self .math_type_cfg_id == '5' :

         self.hide = True

    else :

         self.hide = False


In my view I have:


<field name = "c_math_cfg_id"                                         

                                         required = "math_type_cfg_id == 5"

                                         invisible = "'[('hide', '=', True)]"                                       

                                         

                                         />



The required works but the field will not disappear.

Any help would be greatly appreciated!

Avatar
Discard
Best Answer

Hello, 

try :

//.py

    math_type_cfg_id = fields.Selection([

        ('1', 'Type 1'),

        ('2', 'Type 2'),

        ('3', 'Type 3'),

        ('4', 'Type 4'),

        ('5', 'Type 5'),

    ], string="Math Type")


    hide = fields.Boolean(string='Hide', compute="_compute_hide")


    @api.depends('math_type_cfg_id')

    def _compute_hide(self):

        for record in self:

            record.hide = record.math_type_cfg_id == '5'


// .xml

<field name="c_math_cfg_id"

       required="math_type_cfg_id == '5'"

       invisible="[['hide', '=', True]]"

/>


Thanks

Avatar
Discard
Author Best Answer

I managed to resolve it.

Invisible needs to be before required and then I didn't need the on change function.


Can't close or delete the question as I do not have enough karma

Avatar
Discard
Related Posts Replies Views Activity
4
May 25
1003
2
May 25
4028
1
Mar 25
490
4
Mar 25
3148
3
Feb 25
3778