|
@@ -11,7 +11,8 @@ class Number(ValueElement):
|
|
placeholder: Optional[str] = None,
|
|
placeholder: Optional[str] = None,
|
|
value: Optional[float] = None,
|
|
value: Optional[float] = None,
|
|
format: Optional[str] = None,
|
|
format: Optional[str] = None,
|
|
- on_change: Optional[Callable] = None) -> None:
|
|
|
|
|
|
+ on_change: Optional[Callable] = None,
|
|
|
|
+ validation: Dict[str, Callable] = {}) -> None:
|
|
"""Number Input
|
|
"""Number Input
|
|
|
|
|
|
:param label: displayed name for the number input
|
|
:param label: displayed name for the number input
|
|
@@ -19,6 +20,7 @@ class Number(ValueElement):
|
|
:param value: the initial value of the field
|
|
:param value: the initial value of the field
|
|
:param format: a string like '%.2f' to format the displayed value
|
|
:param format: a string like '%.2f' to format the displayed value
|
|
:param on_change: callback to execute when the input is confirmed by leaving the focus
|
|
:param on_change: callback to execute when the input is confirmed by leaving the focus
|
|
|
|
+ :param validation: dictionary of validation rules, e.g. ``{'Too small!': lambda value: value < 3}``
|
|
"""
|
|
"""
|
|
self.format = format
|
|
self.format = format
|
|
super().__init__(tag='q-input', value=value, on_value_change=on_change)
|
|
super().__init__(tag='q-input', value=value, on_value_change=on_change)
|
|
@@ -27,6 +29,16 @@ class Number(ValueElement):
|
|
self._props['label'] = label
|
|
self._props['label'] = label
|
|
if placeholder is not None:
|
|
if placeholder is not None:
|
|
self._props['placeholder'] = placeholder
|
|
self._props['placeholder'] = placeholder
|
|
|
|
+ self.validation = validation
|
|
|
|
+
|
|
|
|
+ def on_value_change(self, value: Any) -> None:
|
|
|
|
+ super().on_value_change(value)
|
|
|
|
+ for message, check in self.validation.items():
|
|
|
|
+ if check(value):
|
|
|
|
+ self.props(f'error error-message="{message}"')
|
|
|
|
+ break
|
|
|
|
+ else:
|
|
|
|
+ self.props(remove='error')
|
|
|
|
|
|
def _msg_to_value(self, msg: Dict) -> Any:
|
|
def _msg_to_value(self, msg: Dict) -> Any:
|
|
return float(msg['args']) if msg['args'] else None
|
|
return float(msg['args']) if msg['args'] else None
|