Corex. FormField
(Corex v0.2.0)
View Source
Use invalid?/1 when you need an explicit invalid={...} from visible
changeset errors. Prefer auto_invalid on the component for the common case.
Explicit invalid wins over auto_invalid.
See the Forms guide.
Summary
Functions
Assigns a stable optional :id for hook hosts.
Fills an assign from the form field only when the caller left it blank.
Returns true when the field has visible errors.
Functions
@spec assign_errors(map(), Phoenix.HTML.FormField.t()) :: map()
@spec assign_form_field(map(), Phoenix.HTML.FormField.t()) :: map()
@spec assign_ids(map(), Phoenix.HTML.FormField.t()) :: map()
Assigns a stable optional :id for hook hosts.
Derivation order:
- Explicit
:idwhen present and non-empty - Prefixed
:namewhen present (e.g."accordion-country") - Prefixed random id for static usage
Emits a Logger.warning in non-prod when the random fallback is used with
server-driven updates (controlled or a server on_* handler), because a
regenerated id remounts the LiveView hook on the next patch.
Fills an assign from the form field only when the caller left it blank.
assign_new/3 cannot serve here: form_control_attrs/1 declares these attrs
with default: nil, so the key is always present and assign_new/3 would
never run. Blank means nil or "", both of which mean "not given".
@spec invalid?(Phoenix.HTML.FormField.t()) :: boolean()
Returns true when the field has visible errors.
On phx-change validation (form.action == :validate), errors follow
used_input?/1. After a failed save (:insert / :update / …), all
field errors are shown.
<.select field={@form[:country]} invalid={Corex.FormField.invalid?(@form[:country])} />