# `MishkaGervaz.Form.Behaviours.FieldType`
[🔗](https://github.com/mishka-group/mishka_gervaz/blob/v0.0.1-alpha.3/lib/mishka_gervaz/form/behaviours/field_type.ex#L1)

Behaviour for form field type renderers.

A field type is a module that knows how to render an input, sanitize and
parse incoming params, validate the resulting value, and supply a default
UI configuration. The DSL accepts either a built-in token (`:text`,
`:textarea`, `:select`, …) or a module implementing this behaviour.

## Example

    defmodule MyApp.FieldTypes.Color do
      @behaviour MishkaGervaz.Form.Behaviours.FieldType
      use Phoenix.Component

      @impl true
      def render(assigns, _config) do
        ~H"""
        <input type="color" name={@name} value={@value} />
        """
      end

      @impl true
      def sanitize(value, _config), do: value

      @impl true
      def parse_params(value, _config), do: value

      @impl true
      def validate(value, _config), do: {:ok, value}

      @impl true
      def default_ui, do: %{type: :color}
    end

Then use in DSL:

    field :background_color, MyApp.FieldTypes.Color

Built-in implementations live under `MishkaGervaz.Form.Types.Field.*`
(e.g. `MishkaGervaz.Form.Types.Field.Hidden`,
`MishkaGervaz.Form.Types.Field.Relation`) and serve as reference
implementations.

See `MishkaGervaz.Form.Behaviours.Template`,
`MishkaGervaz.Form.Entities.Field`, and the `MishkaGervaz.Form.Types.Field`
directory.

# `assigns`

```elixir
@type assigns() :: map()
```

Phoenix LiveView assigns map passed to `render/2`.

# `config`

```elixir
@type config() :: map()
```

Field configuration map produced by the DSL.

# `rendered`

```elixir
@type rendered() :: Phoenix.LiveView.Rendered.t()
```

Result of a Phoenix render.

# `default_ui`
*optional* 

```elixir
@callback default_ui() :: map()
```

Return the default UI configuration for this field type.

# `parse_params`
*optional* 

```elixir
@callback parse_params(raw_value :: any(), config()) :: any()
```

Parse a raw parameter value into the expected type.

# `render`

```elixir
@callback render(assigns(), config()) :: rendered()
```

Render the form field input.

## Parameters

- `assigns` — Phoenix assigns with field data
- `config`  — field configuration map from the DSL

# `sanitize`
*optional* 

```elixir
@callback sanitize(value :: any(), config()) :: any()
```

Sanitize a raw parameter value for this field type.

Called before validation/submission. Text fields strip HTML tags;
textarea / json / nested fields pass through unchanged.

# `validate`
*optional* 

```elixir
@callback validate(value :: any(), config()) :: {:ok, any()} | {:error, String.t()}
```

Validate a field value.

Returns `{:ok, value}` for valid values or `{:error, message}` for invalid.

---

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