|
@@ -8,11 +8,25 @@ from .value_element import ValueElement
|
|
class ValidationElement(ValueElement):
|
|
class ValidationElement(ValueElement):
|
|
|
|
|
|
def __init__(self, validation: Optional[Union[Callable[..., Optional[str]], Dict[str, Callable[..., bool]]]], **kwargs: Any) -> None:
|
|
def __init__(self, validation: Optional[Union[Callable[..., Optional[str]], Dict[str, Callable[..., bool]]]], **kwargs: Any) -> None:
|
|
- self.validation = validation if validation is not None else {}
|
|
|
|
|
|
+ self._validation = validation
|
|
self._auto_validation = True
|
|
self._auto_validation = True
|
|
self._error: Optional[str] = None
|
|
self._error: Optional[str] = None
|
|
super().__init__(**kwargs)
|
|
super().__init__(**kwargs)
|
|
- self._props['error'] = False # NOTE: reserve bottom space for error message
|
|
|
|
|
|
+ self._props['error'] = None if validation is None else False # NOTE: reserve bottom space for error message
|
|
|
|
+
|
|
|
|
+ @property
|
|
|
|
+ def validation(self) -> Optional[Union[Callable[..., Optional[str]], Dict[str, Callable[..., bool]]]]:
|
|
|
|
+ """The validation function or dictionary of validation functions."""
|
|
|
|
+ return self._validation
|
|
|
|
+
|
|
|
|
+ @validation.setter
|
|
|
|
+ def validation(self, validation: Optional[Union[Callable[..., Optional[str]], Dict[str, Callable[..., bool]]]]) -> None:
|
|
|
|
+ """Sets the validation function or dictionary of validation functions.
|
|
|
|
+
|
|
|
|
+ :param validation: validation function or dictionary of validation functions (``None`` to disable validation)
|
|
|
|
+ """
|
|
|
|
+ self._validation = validation
|
|
|
|
+ self.validate()
|
|
|
|
|
|
@property
|
|
@property
|
|
def error(self) -> Optional[str]:
|
|
def error(self) -> Optional[str]:
|
|
@@ -25,10 +39,11 @@ class ValidationElement(ValueElement):
|
|
|
|
|
|
:param error: The optional error message
|
|
:param error: The optional error message
|
|
"""
|
|
"""
|
|
- if self._error == error:
|
|
|
|
|
|
+ new_error_prop = None if self.validation is None else (error is not None)
|
|
|
|
+ if self._error == error and self._props['error'] == new_error_prop:
|
|
return
|
|
return
|
|
self._error = error
|
|
self._error = error
|
|
- self._props['error'] = error is not None
|
|
|
|
|
|
+ self._props['error'] = new_error_prop
|
|
self._props['error-message'] = error
|
|
self._props['error-message'] = error
|
|
self.update()
|
|
self.update()
|
|
|
|
|
|
@@ -37,14 +52,15 @@ class ValidationElement(ValueElement):
|
|
|
|
|
|
:return: True if the value is valid, False otherwise
|
|
:return: True if the value is valid, False otherwise
|
|
"""
|
|
"""
|
|
- if callable(self.validation):
|
|
|
|
- self.error = self.validation(self.value)
|
|
|
|
|
|
+ if callable(self._validation):
|
|
|
|
+ self.error = self._validation(self.value)
|
|
return self.error is None
|
|
return self.error is None
|
|
|
|
|
|
- for message, check in self.validation.items():
|
|
|
|
- if not check(self.value):
|
|
|
|
- self.error = message
|
|
|
|
- return False
|
|
|
|
|
|
+ if isinstance(self._validation, dict):
|
|
|
|
+ for message, check in self._validation.items():
|
|
|
|
+ if not check(self.value):
|
|
|
|
+ self.error = message
|
|
|
|
+ return False
|
|
|
|
|
|
self.error = None
|
|
self.error = None
|
|
return True
|
|
return True
|