Keepo
Keepo

Reputation: 49

How to hide edit/create button on form by conditions?

I'm a new Odoo developer and I need to hide the edit button when my form is entering a custom state, I need this because of a security problem.

This code in XML does not work when I try to give an attribute to the form.

<record model="ir.ui.view" id="pesan_form_view">
    <field name="name">pesan_service_form</field>
    <field name="model">pesan.service</field>
    <field name="arch" type="xml">
    <form string="Booking Service" attrs="{edit:'false':[('state','in','baru')]}">
    <!-- structure of form -->
</record>

I don't know why it doesn't work.

Upvotes: 3

Views: 9007

Answers (2)

Danila Ganchar
Danila Ganchar

Reputation: 11302

qWeb conditions doesn't work for FormView.

You can check it here(path_to_odoo/addons/web/static/src/js/framework/view.js):

 /**
  * Return whether the user can perform the action ('create', 'edit', 'delete') in this view.
  * An action is disabled by setting the corresponding attribute in the view's main element,
  * like: <form string="" create="false" edit="false" delete="false">
  */
  is_action_enabled: function(action) {
      var attrs = this.fields_view.arch.attrs;
      return (action in attrs) ? JSON.parse(attrs[action]) : true;
  },

This method calls from template FormView.buttons in path_to_odoo/addons/web/static/src/xml/base.xml:

<button t-if="widget.is_action_enabled('edit')"
    type="button"
    class="oe_form_button_edit btn btn-default btn-sm" accesskey="E">
    Edit
</button>

These problems are solved in Odoo with the help of rules(ir.rule object of Odoo)

You can find and edit rules in GUI here: Settings(top menu) -> Security(left menu) -> Access Rules(left menu). Use admin user in debug mode for this.

At the same you can add some rules to data.xml of your module for import. They will be added when you install or update module.

Be careful! Record rules do not apply to the Administrator user.

At the same you can try to expand widget FormView.

Hope this helps you.

Upvotes: 1

Hammad Qureshi
Hammad Qureshi

Reputation: 1146

Try this code.

<record model="ir.ui.view" id="pesan_form_view">
    <field name="name">pesan_service_form</field>
    <field name="model">pesan.service</field>
    <field name="arch" type="xml">
    <form string="Booking Service" attrs="{'edit': [('state', 'in', ['baru'])]}">
    <!-- structure of form -->
</record>

Upvotes: 0

Related Questions