Skua.Field (Skua v0.1.0)

Copy Markdown View Source

Normalizes a Phoenix.HTML.FormField (or raw name/value) into the attributes every Skua form component needs: a sanitized DOM id, the form name, the current value, and the list of errors to display.

This is the bridge that makes field={@form[:email]} "just work" — derived ids/names, changeset errors surfaced only after the user has touched the input (Phoenix.Component.used_input?/1), and a clean escape hatch for components used outside a form.

Components call normalize/1 in their function body:

assigns = Skua.Field.normalize(assigns)

and then read @id, @name, @value, @errors, @show_errors?.

Error translation

Changeset errors arrive as {message, opts} tuples. By default Skua does simple %{key}/%{count} interpolation. To route through your app's Gettext (the usual Phoenix setup), configure a translator:

config :skua, :error_translator, {MyAppWeb.CoreComponents, :translate_error}

Summary

Functions

The errors to display for a field (or explicit :errors), gated on used_input?/1 so a pristine form doesn't flash them. Lets components read errors without going through normalize/1 (which would clobber :value).

Reads :field (a Phoenix.HTML.FormField) plus any explicit :id/:name/:value/:errors overrides and assigns the normalized :id, :name, :value, :errors, and :show_errors? keys.

Translates a single changeset error. Accepts a {msg, opts} tuple or a plain string. Delegates to the configured :error_translator when set.

Functions

display_errors(arg1)

The errors to display for a field (or explicit :errors), gated on used_input?/1 so a pristine form doesn't flash them. Lets components read errors without going through normalize/1 (which would clobber :value).

normalize(assigns)

Reads :field (a Phoenix.HTML.FormField) plus any explicit :id/:name/:value/:errors overrides and assigns the normalized :id, :name, :value, :errors, and :show_errors? keys.

Explicit overrides always win over the field-derived values, so a component can be driven by a bare name/value with no form at all.

translate_error(msg)

Translates a single changeset error. Accepts a {msg, opts} tuple or a plain string. Delegates to the configured :error_translator when set.