howdy/problem
What is wrong with one field of some input.
Rules in howdy/validate and howdy/form report a Problem rather
than a sentence, so the words can be chosen later, in the reader’s
language. JSON error responses carry each problem’s code and its
details, such as maximum, with an English message; a client can
translate by code, and a page rendered on the server can use message
with words for its locale:
let words = problem.translated(locale)
form.error(errors, "name") |> option.map(problem.message(_, words))
translated covers the locales Howdy ships words for; build a
Messages of your own for any other, or to change a few words:
problem.Messages(..problem.english(), required: "can't be blank").
Types
The words for each problem. Each describes the field, as in “Name must not be empty”, and so starts in lower case in English.
pub type Messages {
Messages(
required: String,
repeated: String,
blank: String,
too_short: fn(Int) -> String,
too_long: fn(Int) -> String,
too_small: fn(Int) -> String,
too_large: fn(Int) -> String,
email: String,
integer: String,
one_of: fn(List(String)) -> String,
too_few_items: fn(Int) -> String,
too_many_items: fn(Int) -> String,
file_too_large: fn(String) -> String,
file_type: fn(List(String)) -> String,
)
}
Constructors
-
Messages( required: String, repeated: String, blank: String, too_short: fn(Int) -> String, too_long: fn(Int) -> String, too_small: fn(Int) -> String, too_large: fn(Int) -> String, email: String, integer: String, one_of: fn(List(String)) -> String, too_few_items: fn(Int) -> String, too_many_items: fn(Int) -> String, file_too_large: fn(String) -> String, file_type: fn(List(String)) -> String, )
pub type Problem {
Required
Repeated
Blank
TooShort(minimum: Int)
TooLong(maximum: Int)
TooSmall(minimum: Int)
TooLarge(maximum: Int)
NotEmail
NotInteger
NotOneOf(allowed: List(String))
TooFewItems(minimum: Int)
TooManyItems(maximum: Int)
FileTooLarge(maximum: Int)
FileType(allowed: List(String))
Custom(code: String, message: String)
}
Constructors
-
RequiredNothing was given.
-
RepeatedThe field was given more than once.
-
BlankThe text is empty.
-
TooShort(minimum: Int)The text has fewer than
minimumcharacters. -
TooLong(maximum: Int)The text has more than
maximumcharacters. -
TooSmall(minimum: Int)The number is less than
minimum. -
TooLarge(maximum: Int)The number is more than
maximum. -
NotEmailThe text is not an email address.
-
NotIntegerThe text is not a whole number.
-
NotOneOf(allowed: List(String))The value is not one of
allowed. -
TooFewItems(minimum: Int)The list has fewer than
minimumitems. -
TooManyItems(maximum: Int)The list has more than
maximumitems. -
FileTooLarge(maximum: Int)The file has more than
maximumbytes. -
FileType(allowed: List(String))The file’s type is not one of
allowed, such asimage/*. -
Custom(code: String, message: String)Anything else.
codeis for clients andmessagefor people, such asCustom("taken", "is already taken").
Values
pub fn code(problem: Problem) -> String
A short stable name for the problem, sent to clients as code.
pub fn decoder() -> decode.Decoder(Problem)
Read back the fields to_json_fields writes. An unknown code, or one
without the details it needs, becomes Custom with the message sent.
pub fn describe_size(bytes: Int) -> String
A size in bytes as people write it: 5 MB, 512 KB or 1500 bytes.
pub fn to_json_fields(
problem: Problem,
) -> List(#(String, json.Json))
The JSON fields describing a problem: code, an English message, and
the details the code needs, such as maximum or allowed.
pub fn translated(locale: locale.Locale) -> Messages
The messages for a locale, or for the nearest one Howdy has words for, falling back to English. These were machine translated and are waiting for review by native speakers.