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

Is it possible to use validateF7Input() on an input rendered on the server using renderUI() ? #251

Open
frasemcl opened this issue Dec 21, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@frasemcl
Copy link

I'm facing an issue while trying to apply validateF7Input() to an input generated dynamically on the server using renderUI().

For inputs in the UI from the start, the validateF7Input() works as expected by using the following code in the server:

observe({
  validateF7Input(
    inputId = ns("my_example_id"),
    pattern = "^\\d*\\.?\\d+$", # regex for int or float
    error = "Please enter a number"
  )
})

However, I've encountered difficulties getting it to work for the following input created on the server, which is conditionally shown based on another input:

observeEvent(input$flowmeasured, {
  if (input$flowmeasured == "Yes"){
    x = f7Text(
      inputId = ns("conditional_input"),
      label = "Depth (cm):"
      # value = '0.0'
    )
  } else {
    x = ""
  }
  output$veriflow_srv <- renderUI({
    x %||% ""
  })
})

I tried running observe() only after the same condition changes, but it doesn't seem to enable validateF7Input() for the dynamically generated input:

observeEvent(input$flowmeasured, {
  if (input$flowmeasured == "Yes"){
    observe({
      validateF7Input(
        inputId = ns("conditional_input"),
        pattern = "^\\d*\\.?\\d+$", # regex for int or float
        error = "Please enter a number"
      )
    })
  }
})

Then I tried the same thing within a reactive function, but still no dice:

  addNewValidators <- reactive({
    if (input$flowmeasured == "Yes") {
      validateF7Input(
        inputId = ns("conditional_input"),
        pattern = "^\\d*\\.?\\d+$", # regex for int or float
        error = "Please enter a number"
      )
    }
  })
  observe({
    addNewValidators()
  })

I'm seeking guidance or suggestions on how to effectively use validateF7Input() for inputs generated dynamically on the server. Any insights would be greatly appreciated. Thank you!

@frasemcl
Copy link
Author

frasemcl commented Jan 2, 2024

Update on this, I got it working well enough by using an if statement at the start of my observe block, but I could still use some help if anyone has suggestions. Since input$conditional_input1,2and3 only exists if user answers 'Yes' to a previous input, the following works pretty well, but I'm sure there is a more elegant way to achieve this:

    observe({
      # Thought that req() would work but ended up working better with subsequent if statement
      # req(input$conditional_input1)
      if (!isTruthy(input$conditional_input1) && !isTruthy(input$conditional_input2) && !isTruthy(input$conditional_input3)) {
      print('conditionally running')
      lapply(c("conditional_input1", "conditional_input2", "conditional_input3", function(n) {
        observe({
          validateF7Input(
            inputId = ns(n),
            pattern = "^\\d*\\.?\\d+$", # regex for int or float
            error = "Please enter a number"
          )
        })
      }
      )
      }
    })

Apologies that this example or the examples above aren't given in a demo shiny app.

@DivadNojnarg DivadNojnarg added the enhancement New feature or request label Jan 17, 2024
@frasemcl
Copy link
Author

Thx for your consideration. FYI for my use case I ended up going with your radio input combined with shinyjs show() and hide() and that seems to be working well so far. I decided the falsy check above and the observe() within observe() was too hard to reason about and the input warnings weren't working perfectly.

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

No branches or pull requests

2 participants