Zoi.Form (Zoi v0.10.7)

View Source

Helpers for integrating Zoi objects with Phoenix (or any Phoenix.HTML.FormData) forms.

This module focuses on turning your object schemas into form-friendly schemas and returning a %Zoi.Context{} that implements the Phoenix.HTML.FormData protocol.

Summary

Functions

Parses an object schema and returns the underlying Zoi.Context.

Tweaks an object schema to work nicely with HTML forms.

Functions

parse(obj, input, opts \\ [])

@spec parse(schema :: Zoi.schema(), input :: Zoi.input(), opts :: Zoi.options()) ::
  Zoi.Context.t()

Parses an object schema and returns the underlying Zoi.Context.

The returned context keeps the params in context.input, even when validations fail, so it can be passed directly to Phoenix.Component.to_form/2 or any Phoenix.HTML form helper.

This function automatically normalizes LiveView's map-based array format (with numeric string keys) into regular lists, so context.input always contains clean, manipulable data structures.

prepare(obj)

@spec prepare(Zoi.schema()) :: Zoi.schema()

Tweaks an object schema to work nicely with HTML forms.

This function enables coercion on all nested fields so string params are coerced into their target type and sets the empty values to nil and "", matching how Phoenix sends form inputs.