3

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.

2 Answers2

4

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..." />
Kenly
  • 24,317
  • 7
  • 44
  • 60
0

You can prevent the copy-paste on the text field using jquery by attaching event handlers copy & paste. here is the sample.

 $('.field_text').on("copy paste",function(e) {
     e.preventDefault();
 });
Atul Arvind
  • 16,054
  • 6
  • 50
  • 58