Call sv_optional() to generate a validation function that indicates an input is allowed to not be present. If an sv_optional() rule sees that an input is not present, subsequent rules for that input are skipped and the input is considered valid. Otherwise, the rule simply passes. (sv_optional() will never return a validation error/message.)

By default, the definition of "is present" is based on input_provided().

Child validators (see InputValidator$add_validator()) are not affected by sv_optional() rules in parent validators; only rules in the same validator instance as the sv_optional() will be skipped.

sv_optional(test = input_provided)

Arguments

test

A single-argument function, or single-sided formula (using . to access the value to test), that returns TRUE for success and FALSE for failure.

Value

A function suitable for use as an InputValidator$add_rule() rule.

See also

The sv_required() function, which takes a different approach to field presence.

Other rule functions: compose_rules(), sv_between(), sv_email(), sv_equal(), sv_gte(), sv_gt(), sv_in_set(), sv_integer(), sv_lte(), sv_lt(), sv_not_equal(), sv_numeric(), sv_regex(), sv_required(), sv_url()

Examples

## Only run examples in interactive R sessions
if (interactive()) {

library(shiny)
library(shinyvalidate)

ui <- fluidPage(
  textInput("email", "Email")
)

server <- function(input, output, session) {
  
  # Validation rules are set in the server, start by
  # making a new instance of an `InputValidator()`
  iv <- InputValidator$new()

  # Basic usage: `sv_optional()` is often paired with
  # another `sv_*()` function; below, an email in
  # `input$email` is not required, but if present, it
  # must be valid
  iv$add_rule("email", sv_optional())
  iv$add_rule("email", sv_email())

  # Finally, `enable()` the validation rules
  iv$enable()
}

shinyApp(ui, server)

}