From 04ebbb3b765096d397b0549a83427ca8a963adb5 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Tue, 14 Jun 2016 18:42:49 -0700 Subject: [PATCH] Fix undo on fields with validators with side effects. --- core/events.js | 4 ++++ core/field.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/core/events.js b/core/events.js index 9b9974478..67e210b55 100644 --- a/core/events.js +++ b/core/events.js @@ -550,6 +550,10 @@ Blockly.Events.Change.prototype.run = function(forward) { case 'field': var field = block.getField(this.name); if (field) { + // Run the validator for any side-effects it may have. + // The validator's opinion on validity is ignored. + var validator = field.getValidator(); + validator && validator.call(field, value); field.setValue(value); } else { console.warn("Can't set non-existant field: " + this.name); diff --git a/core/field.js b/core/field.js index 305ecd6d6..4eea423ec 100644 --- a/core/field.js +++ b/core/field.js @@ -232,6 +232,14 @@ Blockly.Field.prototype.setValidator = function(handler) { this.validator_ = handler; }; +/** + * Gets the validation function for editable fields. + * @return {Function} Validation function, or null. + */ +Blockly.Field.prototype.getValidator = function() { + return this.validator_; +}; + /** * Gets the group element for this editable field. * Used for measuring the size and for positioning.