Doggo.Form (Doggo v0.18.4)

Copy Markdown View Source

Helpers for building custom form controls with the same ids and ARIA attributes as the field component.

Summary

Functions

Returns the id of the description of the field with the given input id.

Returns the id of the error list of the field with the given input id.

Returns the aria-describedby value of an input: the ids of the error list and the description, if the field has them, or nil.

Returns the aria-errormessage value of an input: the id of the error list if there are errors, or nil.

Interpolates the values of an error into its message, without translating it.

Functions

field_description_id(id)

(since 0.18.0)
@spec field_description_id(String.t()) :: String.t()

Returns the id of the description of the field with the given input id.

Example

iex> Doggo.Form.field_description_id("pet-name")
"pet-name-description"

field_errors_id(id)

(since 0.18.0)
@spec field_errors_id(String.t()) :: String.t()

Returns the id of the error list of the field with the given input id.

Example

iex> Doggo.Form.field_errors_id("pet-name")
"pet-name-errors"

input_aria_describedby(id, arg2, arg3)

(since 0.18.0)
@spec input_aria_describedby(String.t(), list(), list()) :: String.t() | nil

Returns the aria-describedby value of an input: the ids of the error list and the description, if the field has them, or nil.

description and errors are lists, such as the entries of a description slot and the error messages. The id of the error list comes first, as in the markup of field.

The id of the error list is also the aria-errormessage value (see input_aria_errormessage/2). It is included here as well, since screen readers support aria-errormessage inconsistently.

Examples

iex> Doggo.Form.input_aria_describedby("pet-name", [], [])
nil

iex> Doggo.Form.input_aria_describedby("pet-name", [], ["is too short"])
"pet-name-errors"

iex> Doggo.Form.input_aria_describedby("pet-name", ["Your pet's name"], [])
"pet-name-description"

iex> Doggo.Form.input_aria_describedby(
...>   "pet-name",
...>   ["Your pet's name"],
...>   ["is too short"]
...> )
"pet-name-errors pet-name-description"

input_aria_errormessage(id, arg2)

(since 0.18.0)
@spec input_aria_errormessage(String.t(), list()) :: String.t() | nil

Returns the aria-errormessage value of an input: the id of the error list if there are errors, or nil.

Example

iex> Doggo.Form.input_aria_errormessage("pet-name", ["is too short"])
"pet-name-errors"

translate_error(arg)

(since 0.18.0)
@spec translate_error({String.t(), keyword()}) :: String.t()

Interpolates the values of an error into its message, without translating it.

This is the default of the translate_error build option of field.

Example

iex> Doggo.Form.translate_error({"must have %{count} items", [count: 3]})
"must have 3 items"