DevactionUI.FormField (devaction_ui v0.7.0)

Copy Markdown View Source

What the fields of the library read from a Phoenix.HTML.FormField (field={@form[:title]}): the id, the name, the value and the errors, so a form is written as with the core components or any kit that takes a field, and a field can replace one of them without rewiring the form.

What the caller gives by hand wins: id, name, value and error passed next to field are kept. The errors are only shown once the person has used the input (Phoenix.Component.used_input?/1), as the core components do, so an empty form does not open in red.

The library knows no language, so an error {message, options} is translated by the function the app sets:

# config/config.exs
config :devaction_ui, :translate_error, {MyAppWeb.CoreComponents, :translate_error}

With none set, the %{count} style placeholders of the message are filled and the text is used as it is.

Summary

Functions

The first error of the field when the caller gave none, for the components that show one message.

The messages of a field's errors, translated, or [] while the person has not used the input.

Fills the assigns of a component from its field, keeping what the caller gave by hand, and sets :field_errors ([] without a field). opts

Raises when neither the caller nor a field gave the attributes a component cannot do without.

A value as the text of an input: nil stays nil, a Date or a number becomes its string.

One error of a changeset as text, through the :translate_error of the app when there is one.

Functions

error(arg1)

@spec error(map()) :: String.t() | nil

The first error of the field when the caller gave none, for the components that show one message.

errors(field)

@spec errors(Phoenix.HTML.FormField.t() | nil) :: [String.t()]

The messages of a field's errors, translated, or [] while the person has not used the input.

put(assigns, opts \\ [])

@spec put(map(), keyword()) :: map()

Fills the assigns of a component from its field, keeping what the caller gave by hand, and sets :field_errors ([] without a field). opts:

  • value: — :text (the value as text, for an input), :raw (as it is: a Date, a list), :checkbox (fills checked from a boolean field), :member (fills checked when the component's value is in the field's list, for one box of a group), :radio (fills checked when the component's value is the field's) or nil (leaves the value alone)
  • multiple: — the name ends in []
  • id_suffix: — appended to the field's id, for the boxes and options of a group ("-sge")

require!(assigns, keys, component)

@spec require!(map(), [atom()], String.t()) :: map()

Raises when neither the caller nor a field gave the attributes a component cannot do without.

text(value)

@spec text(term()) :: String.t() | nil

A value as the text of an input: nil stays nil, a Date or a number becomes its string.

translate(message)

@spec translate({String.t(), keyword()} | String.t()) :: String.t()

One error of a changeset as text, through the :translate_error of the app when there is one.