Can you pass other params to Custom Validator?

Tags: #<Tag:0x00007fa99aa00f28>

Is it possible to pass custom parameters to a custom validator?

I was looking at documentation for creating a custom validator here:
https://handsontable.com/docs/7.4.2/Options.html#validator

I’m trying to create a validator for text length, but have it dynamic based on column.
The current method signature looks like:

validator: function(value, callback) {

}

How could I also pass in the “maxlength” to this function so that I can dynamically validate text lengths on a per column basis?

Hey @mhennessy7

the custom renderer can use a regex. Here’s a snipet from StackOverflow that helps you to create such a regex https://stackoverflow.com/questions/11511154/regex-for-maximum-length-in-javascript

1 Like

I am looking for a way to stop/not allow the user to enter more text than a defined max value.
For example, in a phone field, I want to limit amount of text less than 10 characters.

I don’t believe a renderer is a way to stop this, but a validator is? I’m thinking a custom validation function is the best way to enforce this, but I just need a way to pass in a defined max value. Is there no way to do this with a custom validation function?

Maybe something like this http://jsfiddle.net/o61ba3sw/?

That doesn’t really work. Let me ask you this. On a given column, you can pass the validator parameter.

Is it possible to pass a regex expression directly to this validator parameter? Such as?

<HotColumn width={100} validator={"^[A-Z]{1,10}$"}>

I tried this directly, but got error of "No registered validator found under “/^[A-Z]{1,10}$/” name

Hi @mhennessy7

Can you show your implementation?

Here is a documentation about validator - https://handsontable.com/docs/7.4.2/Options.html#validator
As you can see regex is acceptable.

And here is a tutorial relating to data validation - https://handsontable.com/docs/7.4.2/demo-data-validation.html?_ga=2.232351455.1309680488.1584342284-1105142737.1537335787

Thanks for response. I am putting it into a like this:

< – HotColumn key={el.data} title={el.header} data={el.data} validator="/^[0-9]$/" width={el.width} – >

How should I be passing this validator regex when using HotColumn?

You could use validator in this way - https://jsfiddle.net/k4ecf7r5/

Another way - https://codesandbox.io/s/validator-vj1vv

Hey @mhennessy7 Does that work for you?

I see the issue, it does work when using the standard cell. However, I’ve needed a custom renderer so that text doesn’t wrap in the cell… I could do the validation in my custom editor/renderer, however how would I change the cell color based on results?

Again, I’m using react HotColumn and custom renderer here and I don’t see a way to change cell color?

Regex should work either way, once you pass the correct data (validator validates what is shown in the getData() method). Validator does not base its rules on rendered data and cellmeta. If you need to base the validation on rendered data you can only process with the server-side validation.

1 Like