# `Money.Input.Changeset`
[🔗](https://github.com/ex-money/money_input/blob/v0.3.3/lib/money/input/changeset.ex#L2)

Ecto.Changeset helpers for `Money.Input`.

Only compiled when `:ecto` is loaded. Two functions, applied
in order:

* `cast_money/3` — converts a `%{"amount" => ..., "currency"
  => ...}` form submission into a `t:Money.t/0` change. Wraps
  `Money.Input.Cast.cast/2`.

* `validate_money/3` — applies business rules (bounds,
  precision, currency match) to the cast value. Wraps
  `Money.Input.Validator.validate_money/2`.

Plain-number fields (`:integer`, `:decimal`) are validated
by the sibling `Localize.Inputs.Changeset.validate_number/3`.

    schema "products" do
      field :price, Money.Ecto.Composite.Type
    end

    def changeset(product, attrs) do
      product
      |> Ecto.Changeset.cast(attrs, [:price])
      |> Money.Input.Changeset.validate_money(:price,
           min: Money.new(:USD, "0.01"),
           max: Money.new(:USD, 9999))
    end

# `cast_money`

```elixir
@spec cast_money(Ecto.Changeset.t(), atom(), Keyword.t()) :: Ecto.Changeset.t()
```

Casts a nested `%{"amount" => ..., "currency" => ...}` form
submission into a `t:Money.t/0` and puts it on the changeset.

Use this when the field is **not** typed as
`Money.Ecto.Composite.Type` (which casts the same map shape
automatically). Delegates to `Money.Input.Cast.cast/2`, which
is locale-aware via `Money.new/3`, so casting works even in
the Path A fallback when the AutoNumeric JS hook isn't loaded.

### Arguments

* `changeset` is an `Ecto.Changeset`.

* `field` is the field name.

* `options` is a keyword list.

### Options

* `:locale` — locale to parse the amount under. Defaults to
  `Localize.get_locale/0`.

* `:currency` — fallback currency if the submitted map omits
  the `currency` key.

### Returns

* The changeset with a `t:Money.t/0` change put on `field`, or
  with an error added if the amount/currency can't be parsed.

### Examples

    iex> changeset =
    ...>   Ecto.Changeset.cast(
    ...>     {%{}, %{price: :map}},
    ...>     %{"price" => %{"amount" => "1.234,56", "currency" => "EUR"}},
    ...>     [:price]
    ...>   )
    iex> changeset = Money.Input.Changeset.cast_money(changeset, :price, locale: :de)
    iex> Ecto.Changeset.get_change(changeset, :price)
    Money.new(:EUR, "1234.56")

# `validate_money`

```elixir
@spec validate_money(Ecto.Changeset.t(), atom(), Keyword.t()) :: Ecto.Changeset.t()
```

Validates a `t:Money.t/0` field with the rules from
`Money.Input.Validator.validate_money/2`.

### Arguments

* `changeset` is an `Ecto.Changeset`.

* `field` is the field name.

* `options` is a keyword list forwarded to the validator
  (`:min`, `:max`, `:required`, `:currency`).

### Returns

* The changeset, with any errors added.

### Examples

    iex> changeset = Ecto.Changeset.change({%{price: Money.new(:USD, "10")}, %{price: :map}})
    iex> changeset = Money.Input.Changeset.validate_money(changeset, :price, currency: :EUR)
    iex> changeset.errors[:price]
    {"must be EUR", [validation: :currency]}

---

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