Skip to content

Add usage of checkbox answers validator #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions PyInquirer/prompts/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
ScrollOffsets, HSplit
from prompt_toolkit.layout.dimension import LayoutDimension as D
from prompt_toolkit.token import Token
from prompt_toolkit.validation import ValidationError


from .. import PromptParameterException
from ..separator import Separator
Expand All @@ -30,6 +32,8 @@ def __init__(self, choices, pointer_index, **kwargs):
self.unselected_sign = kwargs.pop("unselected_sign", "\u25cb")
self.selected_options = [] # list of names
self.answered = False
self.answered_correctly = True
self.error_message = None
self._init_choices(choices)
super(InquirerControl, self).__init__(self._get_choice_tokens,
**kwargs)
Expand Down Expand Up @@ -160,6 +164,8 @@ def get_prompt_tokens(cli):
tokens.append((Token.Instruction,
' (<up>, <down> to move, <space> to select, <a> '
'to toggle, <i> to invert)'))
if not ic.answered_correctly:
tokens.append((Token.Error, ' Error: %s' % ic.error_message))
return tokens

# assemble layout
Expand Down Expand Up @@ -234,9 +240,14 @@ def _prev():

@manager.registry.add_binding(Keys.Enter, eager=True)
def set_answer(event):
ic.answered = True
# TODO use validator
event.cli.set_return_value(ic.get_selected_values())
try:
validator(ic.get_selected_values())
ic.answered = True
event.cli.set_return_value(ic.get_selected_values())
except ValidationError as e:
ic.error_message = e
ic.answered_correctly = False
event.cli.reset()

return Application(
layout=layout,
Expand Down