Skip to content
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

check.required() not working with input_selectize() #2

Open
sravanpannala opened this issue Jan 13, 2024 · 1 comment
Open

check.required() not working with input_selectize() #2

sravanpannala opened this issue Jan 13, 2024 · 1 comment

Comments

@sravanpannala
Copy link

I'm trying to use shiny-validate for python for checking if the user has selected exactly 5 choices in input_selectize.
The documentation shows that with the current rules, this is not possible.
Also, check.required() is not working with input_selectize at all. That is, no validation is occurring.
Here is my cod

app_ui = ui.page_fluid(
    ui.input_selectize("players","Lineup (Select Exactly 5 players)",player_dict,multiple=True, width="70%")
)

def server(input, output, session):
    iv = InputValidator()
    iv.add_rule("players",check.required())
    iv.enable()
    
   @render.plot(alt="Stat Trends")
    def plot():
        req(iv.is_valid())
        fig, ax = plt.subplots(1,1
        return fig

The red text isn't showing under input_selectize, and the plot is showing when it shouldn't.

@gshotwell
Copy link
Contributor

gshotwell commented Jan 13, 2024

You can define your own checks like this (this isn't documented anywhere yet). The rules for check functions are that they must return a function which takes a value argument, and returns a message if the check fails.

from shiny import App, ui, render, req
from shiny_validate import InputValidator, check

app_ui = ui.page_fluid(
    # ui.input_text("letters", "Letters"),
    ui.input_selectize(
        "letters", "Letters", choices=["A", "B", "C", "D", "E"], multiple=True
    ),
    ui.output_text("greeting"),
)


def n_selected(n=5):
    def inner(value):
        if len(value) != 5:
            return f"Please select {n} letters"

    return inner


def server(input, output, session):
    iv = InputValidator()

    iv.add_rule("letters", n_selected())
    iv.enable()

    @render.text
    def greeting():
        req(iv.is_valid())
        return f"Nice to meet you, {input.name()} <{input.email()}>!"


app = App(app_ui, server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants