与R中的JavaScript闪亮的文本的验证sweetalert2文本输入

问题描述 投票:0回答:2

我从sweetalert升级R中闪亮,至今已成功地获得通过R闪亮的服务器就OK了常规响应/取消按钮的警告消息努力sweetalert2,但现在我坚持下一个类型,即文字输入消息我曾经执行一些验证为:

  • 值发送给R.之前使用的特殊字符

Here你可以在其中找到sweetalert2实现的应用程序。

在新的问题,我试图取代的JavaScript与持有输入消息的消息应用程序:

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'}) };
    }
    });
    };"

这工作,到目前为止,但我不知道如何添加其他支票使用特殊字符(未在文件名中允许的)在我以前的代码我有这行sweetalert(1)工作:

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

但是,当我建立这个,它不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'})} 
}
}
});
};
javascript r shiny sweetalert2
2个回答
1
投票

正如在其他职位答应了,这里是没有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)
© www.soinside.com 2019 - 2024. All rights reserved.