Search documentation of phoenix_kit

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

View Source

Field type definitions and utilities for the Entities system.

This module defines all supported field types for entity definitions, including their properties, validation rules, and rendering information.

## Supported Field Types

### Basic Text Types

- text: Single-line text input
- textarea: Multi-line text area
- email: Email address with validation
- url: URL with validation
- rich_text: Rich HTML editor (TinyMCE/CKEditor-like)

### Numeric Types

- number: Numeric input (integer or decimal)

### Boolean Types

- boolean: True/false toggle or checkbox

### Date/Time Types

- date: Date picker (YYYY-MM-DD format)

### Choice Types

- select: Dropdown selection (single choice)
- radio: Radio button group (single choice)
- checkbox: Checkbox group (multiple choices)

## Usage Examples

    # Get all field types
    field_types = PhoenixKit.Entities.FieldTypes.all()

    # Get field type info
    text_info = PhoenixKit.Entities.FieldTypes.get_type("text")

    # Get field types by category
    basic_types = PhoenixKit.Entities.FieldTypes.by_category(:basic)

    # Check if field type requires options
    PhoenixKit.Entities.FieldTypes.requires_options?("select") # => true

# Summary

## Types

field_category()
field_type()

## Functions

all()

Returns all field types as a map.

boolean_field(key, label, opts \\ [])

Helper to create a boolean field.

by_category(category)

Returns field types grouped by category.

categories()

Returns all categories with their field types.

category_list()

Returns a list of category names with labels.

checkbox_field(key, label, options, opts \\ [])

Helper to create a checkbox field with options.

default_props(type_name)

Gets the default properties for a field type.

email_field(key, label, opts \\ [])

Helper to create an email field.

for_picker()

Returns field types suitable for a field picker UI.

get_type(type_name)

Gets information about a specific field type.

list_types()

Returns a list of all field type names.

new_field(type, key, label, opts \\ [])

Creates a new field definition with default values.

number_field(key, label, opts \\ [])

Helper to create a number field.

radio_field(key, label, options, opts \\ [])

Helper to create a radio button field with options.

requires_options?(type_name)

Checks if a field type requires options to be defined.

rich_text_field(key, label, opts \\ [])

Helper to create a rich text field.

select_field(key, label, options, opts \\ [])

Helper to create a select field with options.

text_field(key, label, opts \\ [])

Helper to create a text field.

textarea_field(key, label, opts \\ [])

Helper to create a textarea field.

valid_type?(type_name)

Checks if a field type exists.

validate_field(field)

Validates a field definition map.

# Types

# field_category()

    @type field_category() :: :basic | :numeric | :boolean | :datetime | :choice

# field_type()

    @type field_type() :: String.t()

# Functions

# all()

Returns all field types as a map.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.all()
    %{"text" => %{name: "text", ...}, ...}

# boolean_field(key, label, opts \\ [])

Helper to create a boolean field.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.boolean_field("active", "Is Active", default: true)
    %{"type" => "boolean", "key" => "active", "label" => "Is Active", "default" => true, ...}

# by_category(category)

Returns field types grouped by category.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.by_category(:basic)
    [%{name: "text", ...}, %{name: "textarea", ...}, ...]

# categories()

Returns all categories with their field types.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.categories()
    %{
      basic: [%{name: "text", ...}, ...],
      numeric: [%{name: "number", ...}],
      ...
    }

# category_list()

Returns a list of category names with labels.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.category_list()
    [
      {:basic, "Basic"},
      {:numeric, "Numeric"},
      ...
    ]

# checkbox_field(key, label, options, opts \\ [])

Helper to create a checkbox field with options.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.checkbox_field("tags", "Tags", ["Featured", "Popular", "New"])
    %{"type" => "checkbox", "key" => "tags", "label" => "Tags", "options" => ["Featured", "Popular", "New"], ...}

# default_props(type_name)

Gets the default properties for a field type.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.default_props("text")
    %{"placeholder" => "", "max_length" => 255}

# email_field(key, label, opts \\ [])

Helper to create an email field.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.email_field("email", "Email Address", required: true)
    %{"type" => "email", "key" => "email", "label" => "Email Address", "required" => true, ...}

# for_picker()

Returns field types suitable for a field picker UI.

Formats the data for use in select dropdowns or type choosers.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.for_picker()
    [
      %{value: "text", label: "Text", category: "Basic", icon: "hero-pencil"},
      ...
    ]

# get_type(type_name)

Gets information about a specific field type.

Returns nil if the type doesn't exist.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.get_type("text")
    %{name: "text", label: "Text", ...}

    iex> PhoenixKit.Entities.FieldTypes.get_type("invalid")
    nil

# list_types()

Returns a list of all field type names.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.list_types()
    ["text", "textarea", "number", ...]

# new_field(type, key, label, opts \\ [])

Creates a new field definition with default values.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.new_field("text", "my_field", "My Field")
    %{
      "type" => "text",
      "key" => "my_field",
      "label" => "My Field",
      "required" => false,
      "default" => "",
      "validation" => %{},
      "placeholder" => "",
      "max_length" => 255
    }

    # With options for choice fields
    iex> PhoenixKit.Entities.FieldTypes.new_field("select", "category", "Category", options: ["Tech", "Business"])
    %{
      "type" => "select",
      "key" => "category",
      "label" => "Category",
      "required" => false,
      "options" => ["Tech", "Business"],
      ...
    }

    # With required flag
    iex> PhoenixKit.Entities.FieldTypes.new_field("text", "name", "Name", required: true)
    %{"type" => "text", "key" => "name", "label" => "Name", "required" => true, ...}

# number_field(key, label, opts \\ [])

Helper to create a number field.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.number_field("age", "Age")
    %{"type" => "number", "key" => "age", "label" => "Age", ...}

# radio_field(key, label, options, opts \\ [])

Helper to create a radio button field with options.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.radio_field("priority", "Priority", ["Low", "Medium", "High"])
    %{"type" => "radio", "key" => "priority", "label" => "Priority", "options" => ["Low", "Medium", "High"], ...}

# requires_options?(type_name)

Checks if a field type requires options to be defined.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.requires_options?("select")
    true

    iex> PhoenixKit.Entities.FieldTypes.requires_options?("text")
    false

# rich_text_field(key, label, opts \\ [])

Helper to create a rich text field.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.rich_text_field("content", "Content", required: true)
    %{"type" => "rich_text", "key" => "content", "label" => "Content", "required" => true, ...}

# select_field(key, label, options, opts \\ [])

Helper to create a select field with options.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.select_field("category", "Category", ["Tech", "Business", "Other"])
    %{"type" => "select", "key" => "category", "label" => "Category", "options" => ["Tech", "Business", "Other"], ...}

    iex> PhoenixKit.Entities.FieldTypes.select_field("status", "Status", ["Active", "Inactive"], required: true)
    %{"type" => "select", "key" => "status", "label" => "Status", "options" => ["Active", "Inactive"], "required" => true, ...}

# text_field(key, label, opts \\ [])

Helper to create a text field.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.text_field("name", "Full Name", required: true)
    %{"type" => "text", "key" => "name", "label" => "Full Name", "required" => true, ...}

# textarea_field(key, label, opts \\ [])

Helper to create a textarea field.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.textarea_field("bio", "Biography")
    %{"type" => "textarea", "key" => "bio", "label" => "Biography", ...}

# valid_type?(type_name)

Checks if a field type exists.

## Examples

    iex> PhoenixKit.Entities.FieldTypes.valid_type?("text")
    true

    iex> PhoenixKit.Entities.FieldTypes.valid_type?("invalid")
    false

# validate_field(field)

Validates a field definition map.

Checks that the field has all required properties and valid values.

## Examples

    iex> field = %{"type" => "text", "key" => "title", "label" => "Title"}
    iex> PhoenixKit.Entities.FieldTypes.validate_field(field)
    {:ok, field}

    iex> invalid_field = %{"type" => "invalid", "key" => "test"}
    iex> PhoenixKit.Entities.FieldTypes.validate_field(invalid_field)
    {:error, "Invalid field type: invalid"}