This question has been flagged

The goal is to hide the states_id field in the res.partner form expect for countries which do have states.

So the idea is to make the field invisible when the country is not in the list of countries with states. I don't know if there are such a country apart from USA.

Also I don't know if I should compare country_id with "United States" or 235. Anyway here is the codeI unsuccessfully tried:

    <record model="ir.ui.view" id="view_partner_form">
        <field name="name">partner.axima</field>
        <field name="model">res.partner</field>
        <field name="inherit_id" ref="base.view_partner_form" />
        <field name="arch" type="xml">
        <xpath expr="//field[@name='state_id']" position="attributes">
                    <attribute name="invisible">country_id not in [235, "United States"]</attribute>
        </xpath>
        </field>
        </record>

But it fails with the following message :

openerp.tools.safe_eval: Cannot eval 'country_id not in [235, "United States"]'
Traceback (most recent call last):
 File "/home/mathieu/src/OpenERP/server/openerp/tools/safe_eval.py", line 284, in safe_eval
 return eval(test_expr(expr, _SAFE_OPCODES, mode=mode), globals_dict, locals_dict)
 File "", line 1, in <module>
 NameError: name 'country_id' is not defined

Is it possible to achieve what I'm trying to do?

Avatar
Discard
Best Answer

The below code to hide state_id base on condition if country_id is null.

<xpath expr="//[@string="state_id"]" position="replace">
<field name='state_id' attrs="{'invisible':[('country_id','=',False)}]"/>  
</xpath>
Avatar
Discard
Author

Thank you, it gave me all needed hints to find the solution with which I'm fine.

Author Best Answer

Here is the solution I retained :

    <xpath expr="//field[@name='city']" position="attributes">
         <!-- avoid country_id and zip_code overlap -->
         <attribute name="style">width:49%%</attribute>
    </xpath>

    <xpath expr="//field[@name='state_id']" position="replace">
    <field name='state_id' 
        class="oe_no_button" 
        placeholder="State"
        style="width: 49%%"
        options='{"no_open": True}' 
        on_change="onchange_state(state_id)" 
        attrs="{'readonly': [('use_parent_address','=',True)]  , 'invisible':['|', ('country_id', '!=', 235), ('country_id','=',False )]}"
        />  
    </xpath>

I took care to keep others attributes defined in server/openerp/addons/base/res/res_partner_view.xml. I also tried to achieve the same result using position="attributes", as follow, but it didn't produce the expected result.

    <xpath expr="//field[@name='state_id']" position="attributes">
         <attribute name="invisible">['|', ('country_id', '!=', 235), ('country_id','=',False )]</attribute>
    </xpath>
Avatar
Discard
Best Answer

for rewrite city, state_id or zip, you must delete the div with the class "address_format", and replace it with a group.

I do not know why, but it's the div that prevents rewrite these fields. 

That's the code : 

<xpath expr="//div[@class='address_format']" position="replace">
  <group col="1">
  <field name="city" placeholder="City" style="width: 40%%" attrs="{'readonly': [('use_parent_address','=',True)]}" nolabel="1"/>
  <field name="state_id" class="oe_no_button" nolabel="1" placeholder="State" style="width: 37%%" options='{"no_open": True}' on_change="onchange_state(state_id)" attrs="{'readonly': [('use_parent_address','=',True)]}"/>
  <field name="zip" placeholder="ZIP" nolabel="1" style="width: 20%%" attrs="{'readonly': [('use_parent_address','=',True)]}"/>
  </group>
</xpath>



Avatar
Discard