question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation doesn't work on checkboxes.

See original GitHub issue

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']

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
JeroenSchmidtcommented, Apr 1, 2020

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

3reactions
langrockcommented, Jul 18, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Checkbox validation not working - Stack Overflow
As @shin mentioned, you need to specify the object of the form during the function call. theForm.declare.checked returns true or false there is ......
Read more >
Custom validation doesn't show when working with check boxes
Custom validation should insert error classes for check boxes. What happens instead? It is not submitting the form (assume invalid form) and not ......
Read more >
MVC 3 Unobtrusive validation does not work with checkboxes ...
Yes Validation is not performed for checkboxes because the programmers that wrote the adapter says that required is different fro mandatory and in...
Read more >
Validation on Checkbox not working - Laracasts
i am trying to get laravel build in validation to work but i now get stuck with some checkboxes. I have some form...
Read more >
Checkbox validation problem since last updates | WordPress.org
Hi,. I realized there's a problem with the checkbox validation since last updates. You can see the contact form in the bottom in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found