diff --git a/core/field.js b/core/field.js index 9b1beb8bb..6b0968e94 100644 --- a/core/field.js +++ b/core/field.js @@ -621,6 +621,12 @@ Blockly.Field.prototype.setValue = function(newValue) { // No change if null. return; } + // Validate input. + var validated = this.callValidator(newValue); + if (validated !== null) { + newValue = validated; + } + // Check for change. var oldValue = this.getValue(); if (oldValue == newValue) { return; diff --git a/core/field_number.js b/core/field_number.js index fd144f04d..10e0efdf0 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -45,10 +45,10 @@ goog.require('Blockly.FieldTextInput'); */ Blockly.FieldNumber = function(opt_value, opt_min, opt_max, opt_precision, opt_validator) { + this.setConstraints(opt_min, opt_max, opt_precision); opt_value = (opt_value && !isNaN(opt_value)) ? String(opt_value) : '0'; Blockly.FieldNumber.superClass_.constructor.call( this, opt_value, opt_validator); - this.setConstraints(opt_min, opt_max, opt_precision); }; goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput); @@ -95,7 +95,6 @@ Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) { this.min_ = isNaN(min) ? -Infinity : min; max = parseFloat(max); this.max_ = isNaN(max) ? Infinity : max; - this.setValue(this.callValidator(this.getValue())); }; /**