0

I'm encountering an issue when working with the validate() function of R in my shiny app.

So this is a user form, I have an action button on the UI side called "go_to_booking_form_1" and I want it to show a sweet alert and go to the next menu tab depending on whether the validation is met or not when clicked.

Currently it will only show the alert when the validation is met but nothing seems to happen if the validation is not met.

Can someone please look at the codes below and see what I did wrong? I also tried using trycatch() but with no success as I'm not that proficient with it.

  completed_tabs <- reactiveValues(tab1 = FALSE, tab2 = FALSE, tab3 = FALSE)


  observeEvent(input$go_to_booking_form_1, {

    validate(
      need(input$parkingRequirement_check, "Please read the notes and check the box."),
      need(input$bookingProcessnotes_check, "Please read the notes and check the box."),
      need(input$eventServicesnotes_check, "Please read the notes and check the box."),
      need(input$contractSettlementnotes_check, "Please read the notes and check the box.")
    )

    completed_tabs$tab1 <- TRUE

    if (completed_tabs$tab1 == TRUE) {
    updateTabsetPanel(session, "sidebar", "booking_form")
    sendSweetAlert(
      session = session,
      title = "Success !!",
      text = "All in order",
      type = "success"
    )}
    else {
      sendSweetAlert(
        session = session,
        title = "Warning !!",
        text = "Please review the mandatory pre-requisites and signal your consent",
        type = "warning"
      )}

  })

Many thanks :)

the following codes is my attempt to use trycatch() but nothings seems to have improved and it's doing the same thing -- alert is only shown when validation is met and nothing happens when validation is not met

completed_tabs <- reactiveValues(tab1 = FALSE, tab2 = FALSE, tab3 = FALSE)

observeEvent(input$go_to_booking_form_1, {
  tryCatch({
    validate(
      need(input$parkingRequirement_check, "Please read the notes and check the box."),
      need(input$bookingProcessnotes_check, "Please read the notes and check the box."),
      need(input$eventServicesnotes_check, "Please read the notes and check the box."),
      need(input$contractSettlementnotes_check, "Please read the notes and check the box.")
    )
    completed_tabs$tab1 <- TRUE
    updateTabsetPanel(session, "sidebar", "booking_form")
    sendSweetAlert(
      session = session,
      title = "Success !!",
      text = "All in order",
      type = "success"
    )
  }, warning = function(w) {
    sendSweetAlert(
      session = session,
      title = "Warning !!",
      text = "Please review the mandatory pre-requisites and signal your consent",
      type = "warning"
    )
  })
})
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    I would not use `validate`. I'd use e.g. `if(input$theCheckbox){ send positive alert } else { send negative alert }`. – Stéphane Laurent Mar 21 '23 at 09:23
  • 1
    `validate()` is meant to be used when rendering outputs to show an error message instead of the output if some conditions are not met. As soon as one of the assertions is not met, the reactive control flow is interrupted and therefore, the alert is not shown. You can think of `validate()` as a graceful alternative to `stopifnot()`. As suggested by Stéphane, the way to make this work is by switching to classical if/else conditions. – Gregor de Cillia Mar 21 '23 at 10:04

0 Answers0