Show SweetAlert warning message when validate() is not met

67 Views Asked by At

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"
    )
  })
})
0

There are 0 best solutions below