PyInquirer icon indicating copy to clipboard operation
PyInquirer copied to clipboard

Validation doesn't work on checkboxes.

Open AndrewOwenMartin opened this issue 5 years ago • 9 comments

It seems to be impossible to raise a validation error on type "checkbox".

Here is the output I get when I run the "checkbox.py" example, and try to trigger "'You must choose at least one topping." error message

$ python checkbox.py
😃 Select toppings  done
{'toppings': []}

Going into more detail, I tried to make a set of questions with all the ways of making a validation error, all checkboxes should fail in all cases, but only the final "input" case shows an error.

from PyInquirer import style_from_dict, Token, prompt, Separator
from PyInquirer import Validator, ValidationError

class FailValidator(Validator):
	
	def validate(self, document):
		
		raise ValidationError(
			message="boogie",
			cursor_position=len(document.text)
		)

def foo(*args, **kwargs):

	raise NotImplementedError("sdfkjsdflj")

questions = [
    {
        'type': 'checkbox',
        'message': 'Select letters',
        'name': 'raise exception in function',
        'choices': [{"name":"a"}, {"name":"b"}, {"name":"c"}],
        'validate': foo,
    },
    {
        'type': 'checkbox',
        'message': 'Select letters',
        'name': 'lambda false',
        'choices': [{"name":"a"}, {"name":"b"}, {"name":"c"}],
        'validate': lambda x: False,
    },
    {
        'type': 'checkbox',
        'message': 'Select letters',
        'name': 'lambda string',
        'choices': [{"name":"a"}, {"name":"b"}, {"name":"c"}],
        'validate': lambda x: "error message",
    },
    {
        'type': 'checkbox',
        'message': 'Select letters',
        'name': 'validator validation error',
        'choices': [{"name":"a"}, {"name":"b"}, {"name":"c"}],
        'validate': FailValidator,
    },
    {
        'type': 'input',
        'name': 'text input',
        'message': 'Get an error on anything but the empty string.',
		'validate': lambda x: x == "" or "Error message",
    },
]

answers = prompt(questions)

for name, answer in answers.items():
	print(name, answer)

Returns


$ python query-cli.py
? Select letters  [a]
? Select letters  [a]
? Select letters  done
? Select letters  done (3 selections)
? Get an error on anything but the empty string.
text input
raise exception in function ['a']
validator validation error ['a', 'b', 'c']
lambda string []
lambda false ['a']

AndrewOwenMartin avatar Apr 10 '19 12:04 AndrewOwenMartin

I came here with the same question. It seems that the answer is in the source code already: https://github.com/CITGuru/PyInquirer/blob/10d53723b36ebc7bba311457ec4afd9747a5c777/PyInquirer/prompts/checkbox.py#L225

vltr avatar May 03 '19 22:05 vltr

Yea, the checkbox doesn't have validator yet. PR is accepted if you figured it out

CITGuru avatar May 05 '19 21:05 CITGuru

LOL, I was going nuts thinking that there was something wrong with my code. Good to know that the validator is not yet implemented.

langrock avatar Jul 18 '19 23:07 langrock

I was looking for the same thing!

abax1 avatar Oct 22 '19 16:10 abax1

I was looking for the same thing! Help...

luisxiaomai avatar Oct 25 '19 03:10 luisxiaomai

This seems like an important feature that should be implemented. I too came here with the same problem. PyInquirer is easy to use and does so much well, but I'm not sure how to get around this one.

mcpalmer1980 avatar Dec 26 '19 16:12 mcpalmer1980

Great, was about to type in the same issue when I realized that the feature doesn't exist yet

pronoym99 avatar Feb 26 '20 20:02 pronoym99

I just encountered this problem. It would indeed be a nice feature to have.

JeroenSchmidt avatar Apr 01 '20 09:04 JeroenSchmidt

For those who need this feature, there is a project called questionary based on this project (but the code has been refactored and tidied up a lot) and seems a bit more up-to-date - the API is very similar.

I've submitted a pull request to the newer repo (https://github.com/tmbo/questionary/pull/48) which adds validation for the checkbox.

kiancross avatar Jun 26 '20 18:06 kiancross