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

Validate user input for type = "input" #45

Closed
JamesBrownSEAMS opened this issue Dec 11, 2020 · 3 comments
Closed

Validate user input for type = "input" #45

JamesBrownSEAMS opened this issue Dec 11, 2020 · 3 comments

Comments

@JamesBrownSEAMS
Copy link

I have a requirement for a new app I am working on and I can't find a way to implment it after reading the docs, I was hoping someone could tell me if it is possible:

Basically, I need to restrict inputs a user can supply. Being able to set the inputType = "number" is helpful in this case as I don't need to validate against non-numeric values, but I would also need to limit the range of allowed values. In one case I am asking the user to supply a year so something like upper and lower limits would work (e.g. 1990 <= input <= 2020).

Is something like this possible?

@daattali
Copy link
Owner

As of last month, if you download the latest CRAN version of the package, you can use any shiny input in the alert (so for this you'd use numericInput or sliderInput). Look at the new documentation.

@JamesBrownSEAMS
Copy link
Author

Thanks for the information, I had not seen the update to the package and the new features are a great enhancement to the package. I put together a very simple test case to explore the new features:

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert(),

  tags$h2("Demo shinyalert"),
  tags$br(),
  fluidRow(
    column(
      width = 6,
      actionButton("shinyalert_popup",
                   "Alert Popup")
    )
  ),
  fluidRow(
    verbatimTextOutput(outputId = "return_value")
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$shinyalert_popup, {
    shinyalert(
      title = "Input control",
      text = tagList(
        numericInput(inputId = "shinyalert_value",
                     label = "",
                     value = 5,
                     min = 0,
                     max = 10,
                     step = 1)
      ),
      html = TRUE,
      inputId = "shinyalert_modal",
      showConfirmButton = TRUE,
      confirmButtonText = "OK"
    )
  })
  
  output$return_value <- renderPrint(input$shinyalert_value)
}

shinyApp(ui = ui, server = server)

It appears that the min/max values are respected when using the up/down controls on the right side of the input (Chrome and FF, they do not appear in Edge or IE) but in all cases I can still type "100" (for example) into the input and it is accepted when pressing the ConfirmButton. I will need to find out how to add more validation to the numericInput control itself or consider using the sliderInput control instead. I do not consider this an issue with shinyalert or the modal it creates so I'll close this case.

@daattali
Copy link
Owner

daattali commented Dec 14, 2020 via email

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