Checkbox validator

96 views
Skip to first unread message

Julian

unread,
Oct 7, 2021, 12:25:49 PM10/7/21
to Blockly
I am trying to make this: https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/checkbox#creating_a_checkbox_validator 

my block definition is:
newBlock.png
The block looks like this:
CheckBox.png

and when I use the code from the link and try this:initCheckBox.png
This doesn't do anything, shouldn't validate be linked to the checkbox somehow. Also I don't know which the Dummy field is, Text will probably be the field created but DUMMY I don't know ( I all ready tried setting checkbox name to DUMMY didn't work ).

Kind regards,

Julian de Bruin


Beka Westberg

unread,
Oct 11, 2021, 12:24:09 PM10/11/21
to blo...@googlegroups.com
Hello Julian,

Sadly the validator doesn't get automatically connected :/ you need to manually connect it using these documented methods. The reason it doesn't get automatically connected is to allow you to have multiple fields with multiple different validators.

For your situation, since you're using a json definition, you'll probably want to do something like this:

```
Blockly.Blocks['newBlock'] = {
  init: function() {
    this.jsonInit(myJsonDef);
    this.getField('MY_CHECKBOX_FIELD_NAME').setValidator(this.validate_.bind(this));
  },

  validate_: function() { /* my custom logic */ }
}
```

I hope that helps! If you have any further questions please reply!
--Beka

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/969125c1-07ad-4733-a21c-2ff70412b3f5n%40googlegroups.com.

Julian

unread,
Oct 22, 2021, 6:47:59 AM10/22/21
to Blockly
Yes this helps thank you very much!

Op maandag 11 oktober 2021 om 18:24:09 UTC+2 schreef bekawe...@gmail.com:
Reply all
Reply to author
Forward
0 new messages