sweetalert2 text input with validation of the text with javascript in R Shiny

痴心易碎 提交于 2019-12-13 20:22:22

问题


I am upgrading from sweetalert to sweetalert2 in R shiny and so far have managed to get the regular response by the R shiny server to ok/cancel buttons in the alert messages working, but now I am stuck with the next type, namely text input messages where I used to perform some validations for:

  • empty
  • use of special characters before sending the value to R.

Here you can find an app in which sweetalert2 is implemented.

in the new problem I'm trying to replace the javascript in that app with a message that holds an input message:

myjava <- "shinyjs.swalFromButton = function(params) { 
      var defaultParams = {
    title: null,
    html : null
    };
    params = shinyjs.getParams(params, defaultParams);
    swal({title : params.title, html : params.html,  
    input: 'text',
    showCancelButton : true,
    showConfirmButton : true,
    closeOnConfirm: false,
    confirmButtonColor: '#339FFF',
    allowOutsideClick: false,
    inputValidator: function(value) {
    if(value === '') { return !value && 'You need to write something!'}
    else {
    var val2= true; 
    Shiny.setInputValue('option2', val2, {priority: 'event'}) };
    }
    });
    };"

This works so far, but I have no clue how to add the other check for use of special characters (which are not allowed in file names) In my old code I had this line for sweetalert (1) working:

 var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
 if(format.test(inputValue)){   
 swal.showInputError('Special characters are not allowed');
 return false;
 }

But when I build this, it doesn't work in sweetalert2:

myjava <- "shinyjs.swalFromButton = function(params) { swalFromButton = function(params) {       var defaultParams = {
    title: null,
    html : null
    };
    params = shinyjs.getParams(params, defaultParams);
    swal({title : params.title, html : params.html,  
    input: 'text',
    showCancelButton : true,
    showConfirmButton : true,
    closeOnConfirm: false,
    confirmButtonColor: '#339FFF',
    allowOutsideClick: false,
  inputValidator: function(value) {
if(value === '') { return !value && 'You need to write something!'}
else {
          var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
                         if(format.test(value)){   
                           return !value && 'Special characters are not allowed'}
                       else {
                         var val2= true; 
Shiny.setInputValue('option2', value, {priority: 'event'})} 
}
}
});
};

回答1:


As promised in the other post, here is a solution without shinyjs:

library(shiny)

js <- "
Shiny.addCustomMessageHandler('sweet',
  function(message) {
    swal({
      title : message.title, 
      html : message.html, 
      input : 'text', 
      showConfirmButton : true,
      confirmButtonText : 'Confirm',
      confirmButtonColor: '#00cc00',
      showCancelButton : true,
      cancelButtonText : 'Cancel',
      cancelButtonColor : '#339fff',
      allowOutsideClick: true,
      allowEscapeKey: true,
      inputValidator: function(value) {
        if(value === '') { 
          return 'You need to write something!'
        } else {
          var format = /\\`|\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\+|\\=|\\[|\\{|\\]|\\}|\\||\\\\|\\'|\\<|\\,|\\.|\\>|\\?|\\/|\"|\\;|\\:/g;
          if(format.test(value)){   
            return 'Special characters are not allowed'
          } 
        }
      }
    })
    .then(function(result){
      if(result.dismiss === swal.DismissReason.cancel) {
        swal('failure');
      } else {
      swal('success');
        Shiny.setInputValue('option1', result.value, {priority: 'event'});
      }
    });
  }
);
"
ui <- basicPage(
  tags$head(tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.all.min.js"),
            tags$link(rel="stylesheet", type="text/css", href = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.min.css"),
            tags$script(js)
  ),
  actionButton("messageButton", "Click me")

)

server <- function(input, output, session){
  observeEvent(input$messageButton, {
    session$sendCustomMessage(type = "sweet",
                              message = list(title = paste('<span style ="color:#339FFF;">An alert with an input text'),
                                             html = "Enter text"))
  })

  observe({print(input$option1)})
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/53618418/sweetalert2-text-input-with-validation-of-the-text-with-javascript-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!