# `PhoenixKit.Modules.Entities.FormBuilder`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/entities/form_builder.ex#L1)

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.Modules.Entities.FormBuilder.build_fields(entity, changeset)

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

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

## Field Type Support

The FormBuilder supports all field types defined in `PhoenixKit.Modules.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.

# `build_field`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/entities/form_builder.ex#L205)

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.Modules.Entities.FormBuilder.build_field(field, changeset)
    # Returns Phoenix.Component field HTML

# `build_fields`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/entities/form_builder.ex#L71)

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.Modules.Entities.FormBuilder.build_fields(entity, changeset)
    # Returns Phoenix.Component form HTML

# `get_field_value`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/entities/form_builder.ex#L871)

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`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/entities/form_builder.ex#L788)

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.Modules.Entities.FormBuilder.validate_data(entity, %{"title" => "Test"})
    {:ok, %{"title" => "Test"}}

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
