Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions codelabs/validation_and_warnings/validation_and_warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Depending on the severity of the issue, this might be sufficient for your custom

![A Blockly workspace with disabled break and continue blocks.](disabled_break.png)

You can disable a block using `this.setEnabled(false)`, although there are some caveats: disabled blocks can't be dragged out of the toolbox flyout, and the act of disabling a block usually adds an event to Blockly's undo history. That's probably not the behavior you want when validating a block, so you can avoid both of these effects with the following code, which you should put inside the change listener function after setting the warning text:
You can disable a block using `this.setDisabledReason(true, 'reason')`, although there are some caveats: disabled blocks can't be dragged out of the toolbox flyout, and the act of disabling a block usually adds an event to Blockly's undo history. That's probably not the behavior you want when validating a block, so you can avoid both of these effects with the following code, which you should put inside the change listener function after setting the warning text:

```js
// Disable invalid blocks (unless it's in a toolbox flyout,
Expand All @@ -248,7 +248,7 @@ You can disable a block using `this.setEnabled(false)`, although there are some
const initialGroup = Blockly.Events.getGroup();
// Make it so the move and the disable event get undone together.
Blockly.Events.setGroup(event.group);
this.setEnabled(valid);
this.setDisabledReason(!valid, 'Invalid range');
Blockly.Events.setGroup(initialGroup);
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Blockly.Extensions.register('list_range_validation', function () {
const initialGroup = Blockly.Events.getGroup();
// Make it so the move and the disable event get undone together.
Blockly.Events.setGroup(event.group);
this.setEnabled(valid);
this.setDisabledReason(!valid, 'Invalid range');
Blockly.Events.setGroup(initialGroup);
}
});
Expand Down
Loading