Prevent cell validation after paste

Tags: #<Tag:0x00007f136644dce0> #<Tag:0x00007f136644da38>

Hi,

I have a use case where our users can choose whether or not to validate their data on paste or not. I’m using the react version and I can’t seem to find a way to identify, during the beforeValidation hook, where I can determine if the validation was caused just after a paste or not.

I want to, if possible, avoid resorting to keeping some external state outside of the component (using useState and render cycle would not happen in time to catch in the beforeValidate hook) and see if there are some internals in hot that could give me access to this information.

Thanks.

Hi @will.busby

Thank you for contacting us. Did you try with beforePaste or afterPaste hooks? I can also recommend this example: https://handsontable.com/docs/react-data-grid/events-and-hooks/#all-available-handsontable-hooks-example to see in which order the hooks are executed.

Hey Adrian,

Thanks for the reply.

Do you mean trying those hooks to set some state so that, in beforeValidate I can check this to prevent the validation?

Or is there internal properties in hot that allow you to detect that pasting has happened?

I guess I could set a custom property on the hotInstance myself maybe?

@will.busby

If you need to check if pasting action has happened it can be done in the afterPaste hook, and if you need to disable validation you can do this with beforeValidate hook. Here’s an example:

https://jsfiddle.net/handsoncode/6bhypa1f/

Thanks @adrian.szymanski

However, I think you misunderstand what I mean. I know you can do that with both of those hooks, what I want to try and do is to prevent validation BUT ONLY after a paste has occurred. So with normal editing, validation is allowed, but after a paste has happened I want to prevent it from happening.

I’ve been working on it and I think I can get this to work by added a property to the React hotInstance via the table Ref, but I am wondering if hot itself has an internal property that I could use as it would be more reliable I think.

Oh ok so there is a source argument to beforeValidate. I think I can get it to work in some way like this:

beforeValidate={(value, row, prop, source) => {
          if (!options.validateOnPaste && source === "CopyPaste.paste") {
            console.log("no validate on paste");
            return 0;
          }

          return value;
        }}

I need to play around with it a bit more but this might be the way to do it

Yes, this is a good approach to do that. Let me know if you would need assistance with anything else.