From 2368f9ab49eafe3709c12daf5ba4d3eb8f856a3f 2015-01-27 20:24:25 From: Jonathan Frederic Date: 2015-01-27 20:24:25 Subject: [PATCH] Validation on the python side --- diff --git a/IPython/html/widgets/widget_float.py b/IPython/html/widgets/widget_float.py index 506fb03..639eb87 100644 --- a/IPython/html/widgets/widget_float.py +++ b/IPython/html/widgets/widget_float.py @@ -38,14 +38,29 @@ class _BoundedFloat(_Float): def __init__(self, *pargs, **kwargs): """Constructor""" super(_BoundedFloat, self).__init__(*pargs, **kwargs) - self._validate('value', None, self.value) - self.on_trait_change(self._validate, ['value', 'min', 'max']) + self._value_changed('value', None, self.value) + self._min_changed('min', None, self.min) + self._max_changed('max', None, self.max) - def _validate(self, name, old, new): - """Validate value, max, min.""" + def _value_changed(self, name, old, new): + """Validate value.""" if self.min > new or new > self.max: self.value = min(max(new, self.min), self.max) + def _max_changed(self, name, old, new): + """Make sure the min is always <= the max.""" + if new < self.min: + raise ValueError("setting max < min") + if new < self.value: + self.value = new + + def _min_changed(self, name, old, new): + """Make sure the max is always >= the min.""" + if new > self.max: + raise ValueError("setting min > max") + if new > self.value: + self.value = new + @register('IPython.FloatText') class FloatText(_Float): diff --git a/IPython/html/widgets/widget_int.py b/IPython/html/widgets/widget_int.py index 73ca5cf..678054e 100644 --- a/IPython/html/widgets/widget_int.py +++ b/IPython/html/widgets/widget_int.py @@ -41,24 +41,28 @@ class _BoundedInt(_Int): def __init__(self, *pargs, **kwargs): """Constructor""" super(_BoundedInt, self).__init__(*pargs, **kwargs) - self.on_trait_change(self._validate_value, ['value']) - self.on_trait_change(self._handle_max_changed, ['max']) - self.on_trait_change(self._handle_min_changed, ['min']) + self._value_changed('value', None, self.value) + self._min_changed('min', None, self.min) + self._max_changed('max', None, self.max) - def _validate_value(self, name, old, new): + def _value_changed(self, name, old, new): """Validate value.""" if self.min > new or new > self.max: self.value = min(max(new, self.min), self.max) - def _handle_max_changed(self, name, old, new): + def _max_changed(self, name, old, new): """Make sure the min is always <= the max.""" if new < self.min: raise ValueError("setting max < min") + if new < self.value: + self.value = new - def _handle_min_changed(self, name, old, new): + def _min_changed(self, name, old, new): """Make sure the max is always >= the min.""" if new > self.max: raise ValueError("setting min > max") + if new > self.value: + self.value = new @register('IPython.IntText') class IntText(_Int):