DynamicForm.Instance (DynamicForm v0.17.6)

Copy Markdown View Source

Configuration struct that defines the complete form structure using SurveyJS-compatible format.

An Instance represents a complete form definition with all its elements (questions and panels), and validators.

SurveyJS Compatibility

This library uses SurveyJS-compatible JSON format for form definitions. See: https://surveyjs.io/form-library/documentation

Example

iex> instance = %DynamicForm.Instance{
...>   id: "contact-form",
...>   title: "Contact Form",
...>   description: "Get in touch with us",
...>   elements: [
...>     %DynamicForm.Instance.Element{
...>       name: "intro",
...>       type: "html",
...>       html: "<h2>Contact Information</h2>"
...>     },
...>     %DynamicForm.Instance.Question{
...>       name: "email",
...>       type: "text",
...>       inputType: "email",
...>       title: "Email Address",
...>       isRequired: true
...>     }
...>   ]
...> }

JSON Encoding/Decoding

Instances can be encoded to JSON and decoded back:

# Encode to JSON
json = Jason.encode!(instance)

# Decode from JSON
instance = DynamicForm.Instance.decode!(json)

# Decode from map
instance = DynamicForm.Instance.decode!(map)

Slot-Defined Instances

Instances can also be built from <:field> slot entries via DynamicForm.form/1 (see DynamicForm.Instance.FromSlots). Questions and elements defined with a slot body carry the raw slot entry in their :slot field so the renderer can call Phoenix.Component.render_slot/2 on it. The :slot field holds a closure, so it is never JSON-encoded; use strip_slots/1 to compare two instances by definition alone.

Summary

Functions

Decodes a JSON string or map into a DynamicForm.Instance struct.

Returns a copy of the instance with all :slot fields removed.

Types

t()

@type t() :: %DynamicForm.Instance{
  description: String.t() | nil,
  elements: [Question.t() | Element.t()],
  id: String.t(),
  inserted_at: DateTime.t() | nil,
  metadata: map(),
  title: String.t() | nil,
  updated_at: DateTime.t() | nil
}

Functions

decode!(data)

Decodes a JSON string or map into a DynamicForm.Instance struct.

Examples

iex> json = ~s({"id": "my-form", "title": "My Form", "elements": []})
iex> DynamicForm.Instance.decode!(json)
%DynamicForm.Instance{id: "my-form", title: "My Form", elements: []}

iex> map = %{"id" => "my-form", "title" => "My Form", "elements" => []}
iex> DynamicForm.Instance.decode!(map)
%DynamicForm.Instance{id: "my-form", title: "My Form", elements: []}

strip_slots(instance)

Returns a copy of the instance with all :slot fields removed.

Slot-defined elements (see DynamicForm.Instance.FromSlots) carry their raw slot entry — including its inner_block closure — in the :slot field. Closures capture template assigns, so two otherwise-identical instances can compare unequal whenever those assigns change. Stripping the slots yields the form definition alone, which compares reliably with ==.