Custom statement input validation

484 views
Skip to first unread message

Jay P.

unread,
May 30, 2017, 2:51:08 PM5/30/17
to Blockly
Hello,
Is there a way to restrict the statement input so it only has 2 children?
I just want to prevent users from adding more than 2 children to a block.


Thanks for any help!

J.

Daaron Dwyer

unread,
May 30, 2017, 2:56:27 PM5/30/17
to Blockly
Why not have a "between" selection with a mutator?

--
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.

Jay P.

unread,
May 30, 2017, 3:06:05 PM5/30/17
to Blockly
Hi Daaron,

I've only been a few minutes checking Blockly ... could you please elaborate a bit more about your suggestion or point me to an URL where they describe a bit more in details the mutator and/or "between" selection?

Thanks!

Daaron Dwyer

unread,
May 30, 2017, 3:28:47 PM5/30/17
to Blockly
Hi Jay,
With only a few minutes into Blockly, this may be an advanced move. That being said, you can see (and edit) the code for the logic_compare block in blocks/logic.js at about line 275. There, you can add a "between" selection.

The mutator is the advanced part. In blocks/lists.js, the lists_getIndex block has a mutator for adding a removing an input block that you can duplicate. The mutationToDom and domToMutation are the important bits. The updateAt_ in domToMutation is the bit where the extra input is added or deleted.

Beyond that, I don't know what you're doing with the actual input-- output to code, interpreted as sql, etc. Whatever that is will need to be able to interpret the new block structure correctly.

Cheers,
Daaron

Erik Pasternak

unread,
May 31, 2017, 1:13:20 PM5/31/17
to Blockly
Hi Jay,

To restrict that to two inputs with that block design you'd need to do something a bit difficult, like add a mutator or write some special connection logic. There are lots of cases you'd have to be aware of, like what happens if someone tries to connect a block between the two or before both of the current blocks.

An easier way if you're still prototyping would be to add output blocks for your boolean checks and make an AND block that takes two inputs:










If you're specifically looking for a range a more targeted block such as the following might work better:

I'm also curious, why do you want to restrict it to only two checks? If you're doing a generic filter it will be far more expressive to allow more than two clauses, which would allow checks like "Patient.Age > 21 && Patient.Age < 65 && Patient.Gender == M"

Cheers,
Erik

Jairo Portela

unread,
May 31, 2017, 1:49:34 PM5/31/17
to blo...@googlegroups.com
Hi Erik,
Thanks for the reply.
This explanation might be a bit long so please bear with me for a while.
We are building a small condition editor for an existing engine. The engine converts these conditions to an SQL Where clause but it has a particular behaviour with "OR" clauses, whenever an "OR" is found then is encloses this current condition and the previous one within 2 parentheses (like this:   (condition1 OR condition2) ). Drawing the OR condition like a container makes the user aware of the way the engine is going to handle those conditions (precedence). In my case I would like to know how easy it is to restrict a block to having only a pre-set number of blocks as children.
Since our engine has these many constraints I would like to be able to restrict the amount of blocks connecting to another one and in some other cases even the "type" of blocks connecting.

Thanks again!


--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/HePkG_5F8Gs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+unsubscribe@googlegroups.com.

Erik Pasternak

unread,
May 31, 2017, 2:14:43 PM5/31/17
to Blockly
Is the intention of your block to become (Patient.Age > 21 OR Patient.Age < 65) ?
--
Erik Pasternak | Master of the House | epas...@google.com     

Jairo Portela

unread,
May 31, 2017, 2:30:16 PM5/31/17
to blo...@googlegroups.com
Yes sir, it is!

Erik Pasternak

unread,
May 31, 2017, 4:44:26 PM5/31/17
to Blockly
Ah, then I have a completely different set of suggestions. =)

Regarding the restrictions you mentioned, one of the goals with most block based apps is to hide the details of syntax and make it more easily understood for beginners. In your example, unless you explain the specific syntax it's not clear why only two items are allowed. You could bypass the restriction by having your generator automatically nest clauses as you add more, so you could generate something like: ((Patient.Age < 21 OR Patient.Age > 65) OR Patient.Gender='M'). This would behave the same as nesting OR blocks in the case where you limit it to two statements.

You might also want to move the 'OR' in the block higher up. It looks like it's connecting the enclosed group to the next statement, not ORing the contents together. If you do want to explicitly limit it to 2 I would still recommend something closer to the standard logic_operation block in Blockly. Stopping at 2 without a visual indication might be confusing to users.
Reply all
Reply to author
Forward
0 new messages