Search documentation of phoenix_kit

# PhoenixKit.Entities.FormBuilder (phoenix_kit v1.6.19)

View Source

Dynamic form builder for entity data forms.

This module generates Phoenix.Component forms based on entity field definitions, enabling dynamic data entry forms that adapt to the entity's schema.

## Usage

    # Generate form fields for an entity
    fields_html = PhoenixKit.Entities.FormBuilder.build_fields(entity, changeset)

    # Generate a single field
    field_html = PhoenixKit.Entities.FormBuilder.build_field(field_definition, changeset)

    # Validate entity data against field definitions
    {:ok, validated_data} = PhoenixKit.Entities.FormBuilder.validate_data(entity, data_params)

## Field Type Support

The FormBuilder supports all field types defined in `PhoenixKit.Entities.FieldTypes`:

- Basic Types: text, textarea, email, url, rich_text
- Numeric Types: number
- Boolean Types: boolean (toggle/checkbox)
- Date Types: date
- Choice Types: select, radio, checkbox (with options)
- Media Types: image, file (upload)
- Relational Types: relation (entity references)

## Form Generation

Forms are generated as Phoenix.Component HTML with proper validation, error handling, and styling consistent with the PhoenixKit design system.

# Summary

## Functions

build_field(field, changeset, opts \\ [])

Builds a single form field based on field definition.

build_fields(entity, changeset, opts \\ [])

Builds form fields HTML for an entire entity.

get_field_value(form, field_key)

Gets the current value of a field from a changeset.

validate_data(entity, data_params)

Validates entity data against field definitions.

# Functions

# build_field(field, changeset, opts \\ [])

Builds a single form field based on field definition.

## Parameters

- `field` - Field definition map
- `changeset` - The changeset for validation and values
- `opts` - Optional configuration

## Examples

    iex> field = %{"type" => "text", "key" => "title", "label" => "Title"}
    iex> changeset = Ecto.Changeset.cast(%{}, %{}, [])
    iex> PhoenixKit.Entities.FormBuilder.build_field(field, changeset)
    # Returns Phoenix.Component field HTML

# build_fields(entity, changeset, opts \\ [])

Builds form fields HTML for an entire entity.

Takes an entity with its field definitions and generates the complete form HTML for data entry.

## Parameters

- `entity` - The entity struct with fields_definition
- `changeset` - The changeset for the entity data
- `opts` - Optional configuration (default: [])

## Options

- `:wrapper_class` - CSS class for field wrapper divs
- `:input_class` - CSS class for input elements
- `:label_class` - CSS class for label elements

## Examples

    iex> entity = %Entities{fields_definition: [
    ...>   %{"type" => "text", "key" => "title", "label" => "Title", "required" => true}
    ...> ]}
    iex> changeset = Ecto.Changeset.cast(%{}, %{}, [])
    iex> PhoenixKit.Entities.FormBuilder.build_fields(entity, changeset)
    # Returns Phoenix.Component form HTML

# get_field_value(form, field_key)

Gets the current value of a field from a changeset.

Helper function to extract field values from changesets or forms for form rendering.

# validate_data(entity, data_params)

Validates entity data against field definitions.

Takes entity field definitions and validates submitted data parameters according to the field types, requirements, and constraints.

## Parameters

- `entity` - The entity with field definitions
- `data_params` - Map of submitted data parameters

## Returns

- `{:ok, validated_data}` - Successfully validated data
- `{:error, errors}` - Validation errors

## Examples

    iex> entity = %Entities{fields_definition: [
    ...>   %{"type" => "text", "key" => "title", "required" => true}
    ...> ]}
    iex> PhoenixKit.Entities.FormBuilder.validate_data(entity, %{"title" => "Test"})
    {:ok, %{"title" => "Test"}}

    iex> PhoenixKit.Entities.FormBuilder.validate_data(entity, %{})
    {:error, %{"title" => ["is required"]}}