Search code examples
javascriptpythonxmlodooodoo-13

Disable copy paste on text field odoo 13


I have a text field.

my_field = fields.Text()

I want to disable copying and paste(ctrl+c, ctrl+v) from intput with java script or python. How i can do it, thanks.


Solution

  • You can extend the FieldText widget and prevent copy and paste events. Create a new widget and bind the events then set the widget attribute on the text field.

    Example: ( The module name is stack_overflow )

    odoo.define('stack_overflow.actions', function (require) {
    "use strict";
    
       var basic_fields = require('web.basic_fields');
       var registry = require('web.field_registry');
    
       var no_copy_paste = basic_fields.FieldText.extend({
    
            events: _.extend({}, basic_fields.FieldText.prototype.events, {
                'copy': '_onCopyPaste',
                'paste': '_onCopyPaste',
            }),
    
            _onCopyPaste: function(ev) {
                ev.preventDefault();
                alert("Copy/Paste Disabled!");
            },
       });
    
       registry.add('no_copy_paste', no_copy_paste);
    });
    

    Add the js file to an asset bundle:

    <template id="assets_backend" name="stack_overflow assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/stack_overflow/static/src/js/actions.js"></script>
        </xpath>
    </template>
    

    Set the widget attribute on the text field:

    <field name="description" widget="no_copy_paste" placeholder="Add a description..." />