Is it possible to set mix/max for value inputs?

443 views
Skip to first unread message

Dennis Baldwin

unread,
Jun 22, 2017, 10:57:19 PM6/22/17
to Blockly
When I use a FieldNumber type I can specify a min and max input like this:

new Blockly.FieldNumber(10, 5, 25, 1), 'speed')

If a user enters the number 3 it will set it to a minimum of 5 and if a user enters the number 30 it will set it to a maximum of 25.

Is it possible to do the same for value inputs? If not, does anyone know the best way to manually validate the input and perhaps display a custom alert? I'm just not sure of the best place to do this validation.

Any help would be greatly appreciated.

Thanks.

Rachel Fenichel

unread,
Jun 23, 2017, 3:49:36 PM6/23/17
to Blockly
Hi Dennis,

Can you clarify what you mean by "value input"?

Here's the Blockly definition, but it doesn't sounds like that's what you're referring to.

Best,
Rachel

Andrew n marshall

unread,
Jun 23, 2017, 4:22:48 PM6/23/17
to blo...@googlegroups.com
Hi Dennis,

If you are referring to the values passed into Blocks (those Rachel referred to), then Blockly doesn't have any support for this.  Nor can we.  Because the value could come from executed code (other blocks), we can't evaluate it in Blockly.  You'll want to add validation and clamping into either your generator code or your API.

This is pretty standard practice.  For example, our text_print block provides an empty string alternative when capturing the value (usually when a block isn't connected).

Blockly.JavaScript['text_print'] = function(block) {
  // Print statement.
  var msg = Blockly.JavaScript.valueToCode(block, 'TEXT',
      Blockly.JavaScript.ORDER_NONE) || '\'\'';
  return 'window.alert(' + msg + ');\n';
};

This is still somewhat static, but you could imagine a generator that wraps a value in a clamp() function, or include that function inside your API (if you wrote the API, as well).


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dennis Baldwin

unread,
Jun 24, 2017, 2:54:21 PM6/24/17
to Blockly
Rachel, sorry if I was unclear on this. I'm relatively new to Blockly and so I'm not very familiar with the terminology.   I think Andrew has me pointed in the right direction. We will do the validation in our generator.

Thanks to both of you for your feedback. Very helpful.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages