A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://r-world-devs.github.io/shinyQueryBuilder/reference/../articles/validation.html below:

validation • shinyQueryBuilder

library(shinyQueryBuilder)
#> Loading required package: queryBuilder

Before the value is sent to Shiny server, rules provided by user are validated. Invalid configuration for the associated rule (e.g. between operator value exceeding valid bounds) is highlighted in the widget, what’s more the value is not sent to the application server.

Validation can be customized on filter level with validate argument, defines as a list of the following arguments:

queryFilter(
  "BirthDate", type = "Date", validation = list(format = "YYYY-MM-DD")
)
queryFilter(
  "Name", type = "character", input = "text", validation = list(format = "[A-Z][a-z]+")
)

queryFilter(
  "digit", type = "integer", validation = list(min = 0, max = 9, step = 1)
)
queryFilter(
  "weekday_abbr", type = "character", input = "textarea", validation = list(min = 3, max = 3)
)

queryFilter(
  "digit", type = "numeric",
  validation = list(
    min = 0, max = 9, step = 1,
    messages = list(
      min = "Minimum value allowed is 0",
      max = "Maximum value allowed is 9",
      step = "Only integers accepted"
    )
  )
)
#> $id
#> [1] "digit"
#> 
#> $type
#> [1] "double"
#> 
#> $validation
#> $validation$min
#> [1] 0
#> 
#> $validation$max
#> [1] 9
#> 
#> $validation$step
#> [1] 1
#> 
#> $validation$messages
#> $validation$messages$min
#> [1] "Minimum value allowed is 0"
#> 
#> $validation$messages$max
#> [1] "Maximum value allowed is 9"
#> 
#> $validation$messages$step
#> [1] "Only integers accepted"
#> 
#> 
#> 
#> $input
#> [1] "number"
queryFilter(
  "Name", type = "character", input = "text",
  validation = list(format = "[A-Z][a-z]+", messages = list(format = "Name should be capitalized"))
)
#> $id
#> [1] "Name"
#> 
#> $type
#> [1] "string"
#> 
#> $input
#> [1] "text"
#> 
#> $validation
#> $validation$format
#> [1] "[A-Z][a-z]+"
#> 
#> $validation$messages
#> $validation$messages$format
#> [1] "Name should be capitalized"

In order to define it, create the function definition as character string and pass it to js function:

queryFilter(
  "fav_letter",
  type = "character",
  input = "text",
  validation = list(
    callback = js(paste0(
      "function(value, Rule) {",
      "var result = true;",
      "is_valid = value.length == 1 && value.toUpperCase() == value;",
      "if (!is_valid) {result = 'Single capital letter allowed only'};",
      "return result;",
      "}",
      collapse = ""
    ))
  )
)
#> $id
#> [1] "fav_letter"
#> 
#> $type
#> [1] "string"
#> 
#> $input
#> [1] "text"
#> 
#> $validation
#> $validation$callback
#> function(value, Rule) {var result = true;is_valid = value.length == 1 && value.toUpperCase() == value;if (!is_valid) {result = 'Single capital letter allowed only'};return result;}

Check the below application to test validation cases:

library(shiny)
pkgload::load_all()

ui <- fluidPage(
  queryBuilderInput(
    "qb",
    filters = list(
      queryFilter(
        "BirthDate", type = "Date", validation = list(format = "YYYY-MM-DD")
      ),
      queryFilter(
        "digit", type = "numeric",
        validation = list(
          min = 0, max = 9, step = 1,
          messages = list(
            min = "Minimum value allowed is 0",
            max = "Maximum value allowed is 9",
            step = "Only integers accepted"
          )
        )
      ),
      queryFilter(
        "Name", type = "character", input = "text",
        validation = list(format = "[A-Z][a-z]+", messages = list(format = "Name should be capitalized"))
      ),
      queryFilter(
        "weekday_abbr", type = "character", input = "textarea", validation = list(min = 3, max = 3)
      ),
      queryFilter(
        "fav_letter",
        type = "character",
        input = "text",
        validation = list(
          callback = js(paste0(
            "function(value, Rule) {",
            "var result = true;",
            "is_valid = value.length == 1 && value.toUpperCase() == value;",
            "if (!is_valid) {result = 'Single capital letter allowed only'};",
            "return result;",
            "}",
            collapse = ""
          ))
        )
      )
    )
  ),
  shiny::verbatimTextOutput("expr")
)

server <- function(input, output, session) {
  output$expr <- renderPrint({
    print(queryToExpr(input$qb))
  })
}

shinyApp(ui, server, options = list(launch.browser = TRUE))

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4