Validating block connections

306 views
Skip to first unread message

Coda Highland

unread,
Apr 15, 2019, 5:21:38 PM4/15/19
to blo...@googlegroups.com
It's clear there's at least some level of validation for block connections in Blockly, since value inputs can reject blocks if the type doesn't match.

I would like to perform validation on a statement input. My block should allow some types of blocks to be connected inside of it while rejecting others.

I've tried implementing an onChange handler that uses block.unplug(true) if it ever detects an invalid child. I've also tried calling workspace.undo() in the same function. Both of these have problems, though:
* block.unplug() kicks the erroneous block into an arbitrary nearby position, and if you try to drop in a stack of blocks it splits them up. 
* workspace.undo() removes new blocks from the workspace if they were dropped in from the toolbox.

In both cases, it doesn't match the behavior of type-based validation, and additionally any shadow blocks that were previously present aren't restored.

Is there a straightforward way to do this that I'm missing?

/s/ Adam

Beka Westberg

unread,
Apr 15, 2019, 5:53:39 PM4/15/19
to Blockly
Hello Adam,

I believe blockly supports this in the same way that it supports value inputs. These docs don't give an example of setting the check on a statement input but they do say "There are three places where typed statements are defined: on the previous and next connections of a block, and on a statement input's connection." So I believe it is possible. Also if you check out the BlockFactory you can see that field blocks can't be dragged into the input space. I believe the line that does this for the demo is here.

I hope that helps! If you have any other questions please reply!

Erik Pasternak

unread,
Apr 15, 2019, 5:58:42 PM4/15/19
to Blockly
Hi Adam,

The issue is that untyped connections will connect to anything (they are essentially wild). Most of the previous and next connections in the demo blocks are untyped, so if you want to create a set of blocks with typed statements you have to modify all of the blocks you are using to add types to the previous and next connections. The Block Factory is an example of this (all of the blocks have types on the previous & next connections).

There's also more details in this feature request.

Cheers,
Erik


On Monday, April 15, 2019 at 2:21:38 PM UTC-7, Coda Highland wrote:

Coda Highland

unread,
Apr 15, 2019, 6:12:40 PM4/15/19
to blo...@googlegroups.com
Thanks Beka, Erik. This is actually a little bit MORE useful than I was expecting. It'll take a little bit of reworking to get it all in line with what I want to do but it should actually make a big improvement in the robustness of my project.

/s/ Adam

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages