CITGuru / PyInquirer

A Python module for common interactive command line user interfaces
MIT License
1.92k stars 236 forks source link

Validation doesn't work on checkboxes. #46

Open AndrewOwenMartin opened 5 years ago

AndrewOwenMartin commented 5 years ago

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']
vltr commented 5 years ago

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

CITGuru commented 5 years ago

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

langrock commented 5 years ago

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

abax1 commented 4 years ago

I was looking for the same thing!

luisxiaomai commented 4 years ago

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

mcpalmer1980 commented 4 years ago

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.

pronoym99 commented 4 years ago

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

JeroenSchmidt commented 4 years ago

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

kiancross commented 4 years ago

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.