I'm trying to programmatically add a custom validator to a FieldNumber inside a shadow block. This shadow block is connected to another block, and both the shadow block and the connection are created programmatically.
Here’s the code to create and connect the shadow block:
I expected that by doing something like the following, the custom validator would be applied whenever a block was dragged from the flyout:
However, I realized that the connection creates a new shadow block from a shadowDom copy, which doesn’t retain this validator. As a workaround, I implemented the following:
This way, every time a shadow block is created, the validator is reapplied to the FieldNumber field. But this feels like a workaround to the shadowDom. Is there a nicer way to do this?
Attached is an example image of the block I'm creating.
Thanks in advance
I'm trying to programmatically add a custom validator to a FieldNumber inside a shadow block. This shadow block is connected to another block, and both the shadow block and the connection are created programmatically.
Here’s the code to create and connect the shadow block:
const shadowBlock = workspace.newBlock(type); shadowBlock.setShadow(true); shadowBlock.setFieldValue(value, fieldName); shadowBlock.initSvg(); connection.connect(shadowBlock.outputConnection);I expected that by doing something like the following, the custom validator would be applied whenever a block was dragged from the flyout:
const shadowField = shadowBlock.getField(fieldName); shadowField?.setValidator(validator);However, I realized that the connection creates a new shadow block from a shadowDom copy, which doesn’t retain this validator.
As a workaround, I implemented the following:
[...]
This way, every time a shadow block is created, the validator is reapplied to the FieldNumber field. But this feels like a workaround to the shadowDom. Is there a nicer way to do this?