-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* A few different types of validation * Rename * Fix test * Updating validation framework * Update lockfile * Ensure validators can be None * Reworking the API a little * Convert Input.Changed to dataclass * Add utility for getting failures as strings * Update an example in Validator docstring * Remove some redundant `pass`es * Renaming variables * Validating Input on submit, attaching result to Submitted event * Testing various validation features * Update snapshots and deps * Styling unfocused -invalid Input differently * Add snapshot test around input validation and associated styles * Validation docs * Tidying validation docs in Input widget reference * Fix mypy issues * Remove __bool__ from Failure, make validator field required * Code review changes * Improving error messages in Validators
- Loading branch information
1 parent
5cb30b5
commit 62fcefb
Showing
14 changed files
with
2,419 additions
and
1,225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
::: textual.validation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from textual import on | ||
from textual.app import App, ComposeResult | ||
from textual.validation import Function, Number, ValidationResult, Validator | ||
from textual.widgets import Input, Label, Pretty | ||
|
||
|
||
class InputApp(App): | ||
# (6)! | ||
CSS = """ | ||
Input.-valid { | ||
border: tall $success 60%; | ||
} | ||
Input.-valid:focus { | ||
border: tall $success; | ||
} | ||
Input { | ||
margin: 1 1; | ||
} | ||
Label { | ||
margin: 1 2; | ||
} | ||
Pretty { | ||
margin: 1 2; | ||
} | ||
""" | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Label("Enter an even number between 1 and 100 that is also a palindrome.") | ||
yield Input( | ||
placeholder="Enter a number...", | ||
validators=[ | ||
Number(minimum=1, maximum=100), # (1)! | ||
Function(is_even, "Value is not even."), # (2)! | ||
Palindrome(), # (3)! | ||
], | ||
) | ||
yield Pretty([]) | ||
|
||
@on(Input.Changed) | ||
def show_invalid_reasons(self, event: Input.Changed) -> None: | ||
# Updating the UI to show the reasons why validation failed | ||
if not event.validation_result.is_valid: # (4)! | ||
self.query_one(Pretty).update(event.validation_result.failure_descriptions) | ||
else: | ||
self.query_one(Pretty).update([]) | ||
|
||
|
||
def is_even(value: str) -> bool: | ||
try: | ||
return int(value) % 2 == 0 | ||
except ValueError: | ||
return False | ||
|
||
|
||
# A custom validator | ||
class Palindrome(Validator): # (5)! | ||
def validate(self, value: str) -> ValidationResult: | ||
"""Check a string is equal to its reverse.""" | ||
if self.is_palindrome(value): | ||
return self.success() | ||
else: | ||
return self.failure("That's not a palindrome :/") | ||
|
||
@staticmethod | ||
def is_palindrome(value: str) -> bool: | ||
return value == value[::-1] | ||
|
||
|
||
app = InputApp() | ||
|
||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.