From 5da820223721aeb88302fafa78824d3245278ef1 Mon Sep 17 00:00:00 2001 From: Felix Mallinder Date: Fri, 20 Nov 2026 15:01:32 +0000 Subject: [PATCH 1/2] ValidatedEntryBox: validate as the value changes Validation only ran on focus out and Return, so a success tick stayed while an invalid value was typed. The entry now validates on every change (an empty optional entry shows no mark). validate_on_change=False keeps the old behaviour. Closes #271 Co-Authored-By: Claude Opus 5.5 --- .../widgets/validated_entry_box.py | 12 +++++++++++- .../Python/tests/test_bhom_tkinter_ui.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py index a6b8818..57b6fa8 100644 --- a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py +++ b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py @@ -35,6 +35,7 @@ def __init__( required: bool = True, custom_validator: Optional[Callable[[Any], tuple[bool, str]]] = None, on_validate: Optional[Callable[[bool], None]] = None, + validate_on_change: bool = True, **kwargs ) -> None: """ @@ -56,6 +57,9 @@ def __init__( required: Whether the field is required custom_validator: Custom validation function that returns (is_valid, error_message) on_validate: Callback function called after validation with validation result + validate_on_change: Validate as the user types, so an invalid value shows its error (and loses + the success mark) straight away. If False, validation runs only on focus out and Return. + An empty optional entry shows neither mark. """ super().__init__(parent, **kwargs) @@ -76,6 +80,7 @@ def __init__( self.required = required self.custom_validator = custom_validator self.on_validate = on_validate + self.validate_on_change = validate_on_change self.default_value = default_value # Store the external variable (any tkinter var type) @@ -139,7 +144,12 @@ def __init__( self.variable.trace_add("write", self._on_value_change) def _on_value_change(self, *_) -> None: - """Fire the on_change callback when the entry value changes.""" + """Validate (if enabled), then fire the on_change callback when the entry value changes.""" + if self.validate_on_change: + if not self.required and not self.get(): + self.clear_error() + else: + self.validate() self._fire_on_change(self.get()) def _sync_to_external(self, *_) -> None: diff --git a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py index 9379fe3..bf12657 100644 --- a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py +++ b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py @@ -291,6 +291,24 @@ def fail_validator(_v): ok, msg, sev = custom_box.validate() assert ok is False and msg == "Custom failed" + # Validation runs as the value changes: an invalid value loses the success mark without focus out + live_box = ValidatedEntryBox(parent, value_type=float, min_value=0.0, max_value=100.0, required=False) + live_box.build() + live_box.set("50") + assert live_box.success_label.get() == "✓" + live_box.set("test") + assert live_box.success_label.get().strip() == "" and live_box.error_label.get() == "Must be a valid number" + live_box.set("150") + assert live_box.error_label.get().startswith("Must be between") + live_box.set("") + assert live_box.success_label.get().strip() == "" and live_box.error_label.get().strip() == "" + + # validate_on_change=False keeps the old behaviour: no mark until validate() is called + lazy_box = ValidatedEntryBox(parent, value_type=float, required=False, validate_on_change=False) + lazy_box.build() + lazy_box.set("test") + assert lazy_box.error_label.get().strip() == "" + def test_rebuild(): """Verify that rebuild() re-places widgets and reflects mutations to self.widgets.""" From d8751af2db9865f7c500cc060aebbc89787324f1 Mon Sep 17 00:00:00 2001 From: Felix Mallinder Date: Mon, 23 Nov 2026 12:16:25 +0000 Subject: [PATCH 2/2] Address review: show errors on commit, not while typing Live validation now only clears a stale success mark and reports validity to on_validate; errors show on focus out / Return. Clearing an optional entry reports valid, and validate() no longer ticks an empty optional entry. Co-Authored-By: Claude Sonnet 5.5 --- .../widgets/validated_entry_box.py | 13 ++++++------ .../Python/tests/test_bhom_tkinter_ui.py | 20 ++++++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py index 57b6fa8..3e8b7cf 100644 --- a/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py +++ b/Python_Engine/Python/src/python_toolkit/bhom_tkinter/widgets/validated_entry_box.py @@ -57,8 +57,9 @@ def __init__( required: Whether the field is required custom_validator: Custom validation function that returns (is_valid, error_message) on_validate: Callback function called after validation with validation result - validate_on_change: Validate as the user types, so an invalid value shows its error (and loses - the success mark) straight away. If False, validation runs only on focus out and Return. + validate_on_change: Validate as the user types, so a stale success mark is removed straight away + and `on_validate` follows the value. Errors are not shown while typing, only on focus out + and Return. If False, validation runs only on focus out and Return. An empty optional entry shows neither mark. """ super().__init__(parent, **kwargs) @@ -146,10 +147,10 @@ def __init__( def _on_value_change(self, *_) -> None: """Validate (if enabled), then fire the on_change callback when the entry value changes.""" if self.validate_on_change: - if not self.required and not self.get(): + is_valid = self.validate()[0] + if not is_valid: + # Keep the error for focus out / Return: the value may just be partly typed. self.clear_error() - else: - self.validate() self._fire_on_change(self.get()) def _sync_to_external(self, *_) -> None: @@ -231,7 +232,7 @@ def validate(self) -> tuple[bool, Optional[str], Optional[Literal['info', 'warni # If not required and empty, it's valid if not self.required and not value_str: - self._show_success() + self.clear_error() self._call_validate_callback(True) base_result = (True, None, None) return self.apply_validation(base_result) diff --git a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py index bf12657..d996a04 100644 --- a/Python_Engine/Python/tests/test_bhom_tkinter_ui.py +++ b/Python_Engine/Python/tests/test_bhom_tkinter_ui.py @@ -292,16 +292,26 @@ def fail_validator(_v): assert ok is False and msg == "Custom failed" # Validation runs as the value changes: an invalid value loses the success mark without focus out - live_box = ValidatedEntryBox(parent, value_type=float, min_value=0.0, max_value=100.0, required=False) + results = [] + live_box = ValidatedEntryBox( + parent, value_type=float, min_value=100.0, max_value=500.0, required=False, on_validate=results.append + ) live_box.build() - live_box.set("50") + live_box.set("300") assert live_box.success_label.get() == "✓" - live_box.set("test") - assert live_box.success_label.get().strip() == "" and live_box.error_label.get() == "Must be a valid number" - live_box.set("150") + # Partly typed / invalid values lose the tick but do not show an error while typing + live_box.set("3") + assert live_box.success_label.get().strip() == "" and live_box.error_label.get().strip() == "" + assert results[-1] is False + # The error shows once the user commits the value (focus out / Return) + live_box.validate() assert live_box.error_label.get().startswith("Must be between") + # Clearing an optional entry shows no marks and reports valid, so callers can re-enable Submit live_box.set("") assert live_box.success_label.get().strip() == "" and live_box.error_label.get().strip() == "" + assert results[-1] is True + live_box.validate() + assert live_box.success_label.get().strip() == "" and results[-1] is True # validate_on_change=False keeps the old behaviour: no mark until validate() is called lazy_box = ValidatedEntryBox(parent, value_type=float, required=False, validate_on_change=False)