sv_required {shinyvalidate}R Documentation

Validate that the field is present

Description

Call sv_required() to generate a validation function that ensures an input value is present. By default, the definition of "is present" is based on input_provided().

Usage

sv_required(message = "Required", test = input_provided)

Arguments

message

The validation error message to be displayed if the test does not pass.

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_optional() 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_optional(), sv_regex(), sv_url()

Examples

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

library(shiny)
library(shinyvalidate)

ui <- fluidPage(
  textInput("name", "Name")
)

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: ensure that `input$name` is present,
  # and return a terse validation message if not
  iv$add_rule("name", sv_required())

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

shinyApp(ui, server)

}

# There are some alternatives to the above example,
# and the following snippets can serve to replace
# the `iv$add_rule(...)` statement

# (1) Providing a custom message to display
# when validation fails:

# iv$add_rule("email", sv_required("An email is required"))

# (2) Providing a `test` argument to change
# the definition of "is present"; in this
# snippet, any non-NULL value will be accepted:

# iv$add_rule("choices", sv_required(test = is.null))


[Package shinyvalidate version 0.1.3 Index]