RED.validators.regex() custom message?

I'm using something like this to validate a colour picker input -

stroke: { value: "#ff0000", validate: RED.validators.regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/) }

I did a bit of digging around on the forum and it seems to be a common issue, that the red highlighting is applied to a hidden object.

I tried (unsuccessfully) to target the button border
image

Failing that I vaguely remember reading that it is possible to pass some options to the validator function so that - invalid input pattern can be replaced by something more helpful ?

I cannot find anywhere now, so I may have dream it :wink:

Is this possible to change this message somehow ?

I'm not aware of a common issue here. Can you raise an issue on github? The color picker should highlight errors properly - that's for us to fix.

It is not possible to customise the messages returned by the validator - but you could wrap it with your own code to do so. Something like... (untested, but I think should do it...):

validate: (function () {
   const validateFunction = RED.validators.regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/);
   return function (v, opt) {
      const result = validateFunction(v,opt);
      if (typeof result === 'string') {
         return 'my custom error message'
      }
      return result
   }
})()
1 Like

Thanks for getting back Nick.

Perhaps common is a bit strong :wink: but there were a few along similar lines -

As you probably noticed I'm working a souped-up version of your annotate node.

As there is the possibility to directly type a hex value into the colour picker, I though it best to validate this. However although it marks an error on the node, the input element that gets the error class is a hidden object.

The code you shared should be a least a help to know which picker has the incorrect value, thanks.