PhoenixKit.Entities.FieldTypes (phoenix_kit v1.5.2)

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

Functions

Returns all field types as a map.

Returns field types grouped by category.

Returns all categories with their field types.

Returns a list of category names with labels.

Gets the default properties for a field type.

Returns field types suitable for a field picker UI.

Gets information about a specific field type.

Returns a list of all field type names.

Creates a new field definition with default values.

Checks if a field type requires options to be defined.

Checks if a field type exists.

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", ...}, ...}

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"},
  ...
]

default_props(type_name)

Gets the default properties for a field type.

Examples

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

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)

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
}

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

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"}