WardrikWeb.Form (wardrik_web v0.1.0-rc.1)

Copy Markdown

Builds a form from a JSON Schema, and reads the submitted form back into a value.

SchemaForm
string, number, boolean, enuma field
objecta fieldset
array of objectsa group you can repeat
oneOfa choice between the alternatives
string with "writeOnly": truea password field
<WardrikWeb.Form.fields schema={schema} params={@params} />

{:ok, input} = WardrikWeb.Form.read(schema, params["input"])
  • Fields are named input[...]. The submitted params hold them under "input".
  • params/2 fills the form with an existing value, for example a saved definition.
  • If the form has repeatable groups, handle the "form-add" and "form-remove" events with handle/3. Update @params on phx-change.
  • read/2 leaves out empty fields.

Summary

Types

The kind of form field a schema produces.

Functions

Adds an empty group to the array of objects at path in params.

Renders the fields for an object schema, filled with params.

Handles the "form-add" and "form-remove" events. Returns the updated params.

Returns the kind of field for schema. Long text fields, like instructions, get a text area.

Returns the params that fill a form for schema with value.

Reads submitted params as a value of schema. Empty fields are left out. Returns :none if every field is empty.

Removes the group at path in params.

Types

kind()

@type kind() ::
  :text
  | :long
  | :integer
  | :number
  | :switch
  | :choice
  | :const
  | :lines
  | :checks
  | :list
  | :object
  | :union
  | :json
  | :empty

The kind of form field a schema produces.

Functions

add(params, path)

@spec add(map() | nil, [String.t()]) :: map()

Adds an empty group to the array of objects at path in params.

fields(assigns)

Renders the fields for an object schema, filled with params.

Use :only or :except to choose which top-level properties to show. Required fields come first.

Attributes

  • schema (:map) (required)
  • params (:any) - Defaults to %{}.
  • only (:list) - Defaults to nil.
  • except (:list) - Defaults to [].
  • target (:any) - The phx-target of the add and remove buttons. Defaults to nil.

handle(arg1, arg2, params)

@spec handle(String.t(), map(), map() | nil) :: map() | nil

Handles the "form-add" and "form-remove" events. Returns the updated params.

Returns nil for any other event.

kind(schema, name \\ nil)

@spec kind(map(), String.t() | nil) :: kind()

Returns the kind of field for schema. Long text fields, like instructions, get a text area.

params(schema, value)

@spec params(map(), term()) :: term()

Returns the params that fill a form for schema with value.

read(schema, params)

@spec read(map(), term()) :: {:ok, term()} | :none | {:error, String.t()}

Reads submitted params as a value of schema. Empty fields are left out. Returns :none if every field is empty.

remove(params, path)

@spec remove(map() | nil, [String.t()]) :: map()

Removes the group at path in params.