Class: Snails::Form::Errors

Inherits:
Object show all
Defined in:
runtime/snails/form/errors.rbs,
runtime/snails/form/errors.rb

Constant Summary collapse

DEFAULT_MESSAGES =

The messages core ships for the built-in rule keys, so an app without I18n still answers real text and an app with one can override per form through message_scope.

{
  blank: "can't be blank",
  taken: "has already been taken",
  invalid: "is invalid",
  inclusion: "is not included in the list",
  exclusion: "is reserved",
  too_short: "is too short (minimum is %{count} characters)",
  too_long: "is too long (maximum is %{count} characters)",
  wrong_length: "is the wrong length (should be %{count} characters)",
  confirmation: "doesn't match %{attribute}",
}

Instance Method Summary collapse

Constructor Details

#initialize(form) ⇒ void

start the list on its owning form.

Parameters:

  • form (Snails::Form) —

    the owning form (message scope and attribute display names come from it)



29
# File 'runtime/snails/form/errors.rb', line 29

def initialize: (Snails::Form form) -> void

Instance Method Details

#add(attribute, message, **options) ⇒ void

append one error. message is display text, or a Symbol key resolved under the form's message scope; **options are interpolation values for that message (count:, attribute:).

Examples:

errors.add(:email, :taken)
errors.add(:password, :too_short, count: 8)
errors.add(:base, "something went wrong")

Parameters:

  • attribute (Symbol) —

    a declared field, or :base for an error that belongs to the whole input (no name prefix)

  • message (String, Symbol) —

    display text or message key

  • options (Hash) —

    interpolation values for the message

Raises:

  • (ArgumentError) —

    a non-Symbol attribute, a name that is not a declared field, or a Symbol key nothing can resolve — an error that would render empty must fail loud here



50
# File 'runtime/snails/form/errors.rb', line 50

def add: (untyped attribute, (String | Symbol) message, **untyped options) -> void

#any? ⇒ bool

at least one error recorded.

Returns:

  • (bool)


89
# File 'runtime/snails/form/errors.rb', line 89

def any?: () -> bool

#empty? ⇒ bool

no errors recorded.

Returns:

  • (bool)


82
# File 'runtime/snails/form/errors.rb', line 82

def empty?: () -> bool

#full_messages ⇒ Array[String]

the display strings, in error order (the ordered list an HTTP 422 detail ships).

Returns:



66
# File 'runtime/snails/form/errors.rb', line 66

def full_messages: () -> Array[String]

#join(separator = ", ") ⇒ String

full_messages.join: the one call a response body needs (errors.join("\n")).

Parameters:

  • separator (String) (defaults to: ", ") —

    the joiner

Returns:



75
# File 'runtime/snails/form/errors.rb', line 75

def join: (?String separator) -> String

#on(attribute) ⇒ Array[String]

the raw (unprefixed) messages recorded for one attribute. A non-Symbol name is a caller bug: it would answer an empty list for a field that does have messages, so the entry guard is loud here as well.

Parameters:

  • attribute (Symbol) —

    the attribute

Returns:

Raises:

  • (ArgumentError) —

    a non-Symbol attribute



107
# File 'runtime/snails/form/errors.rb', line 107

def on: (untyped attribute) -> Array[String]

#size ⇒ Integer

how many errors are recorded.

Returns:



96
# File 'runtime/snails/form/errors.rb', line 96

def size: () -> Integer