Browse Source

Merge pull request #2227 from ghbm-itk/main

ValdationElement.validate now returns a bool
Falko Schindler 1 year ago
parent
commit
2462a6e2b3
2 changed files with 8 additions and 5 deletions
  1. 6 5
      nicegui/elements/mixins/validation_element.py
  2. 2 0
      tests/test_input.py

+ 6 - 5
nicegui/elements/mixins/validation_element.py

@@ -15,16 +15,17 @@ class ValidationElement(ValueElement):
         """The latest error message from the validation functions."""
         """The latest error message from the validation functions."""
         return self._error
         return self._error
 
 
-    def validate(self) -> None:
+    def validate(self) -> bool:
         """Validate the current value and set the error message if necessary."""
         """Validate the current value and set the error message if necessary."""
         for message, check in self.validation.items():
         for message, check in self.validation.items():
             if not check(self.value):
             if not check(self.value):
                 self._error = message
                 self._error = message
                 self.props(f'error error-message="{message}"')
                 self.props(f'error error-message="{message}"')
-                break
-        else:
-            self._error = None
-            self.props(remove='error')
+                return False
+
+        self._error = None
+        self.props(remove='error')
+        return True
 
 
     def _handle_value_change(self, value: Any) -> None:
     def _handle_value_change(self, value: Any) -> None:
         super()._handle_value_change(value)
         super()._handle_value_change(value)

+ 2 - 0
tests/test_input.py

@@ -64,11 +64,13 @@ def test_input_validation(screen: Screen):
     element.send_keys('John')
     element.send_keys('John')
     screen.should_contain('Too short')
     screen.should_contain('Too short')
     assert input_.error == 'Too short'
     assert input_.error == 'Too short'
+    assert not input_.validate()
 
 
     element.send_keys(' Doe')
     element.send_keys(' Doe')
     screen.wait(0.5)
     screen.wait(0.5)
     screen.should_not_contain('Too short')
     screen.should_not_contain('Too short')
     assert input_.error is None
     assert input_.error is None
+    assert input_.validate()
 
 
 
 
 def test_input_with_multi_word_error_message(screen: Screen):
 def test_input_with_multi_word_error_message(screen: Screen):