Skip to content

Commit bddac79

Browse files
committed
fixed issue 1715
1 parent 74765ef commit bddac79

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

examples/test1715.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from prompt_toolkit.shortcuts import input_dialog
2+
from prompt_toolkit.validation import Validator
3+
4+
5+
def is_number(text):
6+
return text.isdigit()
7+
8+
9+
validator = Validator.from_callable(
10+
is_number,
11+
error_message='This input contains non-numeric characters')
12+
13+
age = input_dialog(title="Age", text="Your age:",
14+
validator=validator).run()
15+
16+
print(f"Your age: {age}")

src/prompt_toolkit/shortcuts/dialogs.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from prompt_toolkit.layout.containers import AnyContainer, HSplit
1919
from prompt_toolkit.layout.dimension import Dimension as D
2020
from prompt_toolkit.styles import BaseStyle
21-
from prompt_toolkit.validation import Validator
21+
from prompt_toolkit.validation import Validator, ValidationError
2222
from prompt_toolkit.widgets import (
2323
Box,
2424
Button,
@@ -124,7 +124,14 @@ def accept(buf: Buffer) -> bool:
124124
return True # Keep text.
125125

126126
def ok_handler() -> None:
127-
get_app().exit(result=textfield.text)
127+
if validator is None:
128+
get_app().exit(result=textfield.text)
129+
else:
130+
try:
131+
textfield.validator.validate(textfield.document)
132+
get_app().exit(result=textfield.text)
133+
except ValidationError:
134+
get_app().layout.focus(textfield)
128135

129136
ok_button = Button(text=ok_text, handler=ok_handler)
130137
cancel_button = Button(text=cancel_text, handler=_return_none)

0 commit comments

Comments
 (0)