Add_error for embedded changeset

How exactly i can add an error into nested/embedded changeset? Don’t think add_error can do this.
Context: regular changeset validation already happened and i’m doing some external check

defmodule Thing do
  schema "things" do
    embeds_one :config, ThingConfig, on_replace: :update
defmodule ThingConfig do
  @primary_key false
  embedded_schema do
    field :param, :string
# ??
ch1 = Ecto.Changeset.add_error(ch.changes.config, :param, "error")
# ??????
Ecto.Changeset.put_embed(ch, :config, ch1)

on insert it works, on update - error:

Since you have set :on_replace to :update, you are only allowed
to update the existing entry by giving updated fields as a map or
keyword list or set it to nil.

update_in(ch.changes.config, fn changeset -> 
  Ecto.Changeset.add_error(changeset, :param, "error")

Just be aware that this won’t make the parent become valid?: false.

1 Like

Perfect! Thank you

Well, this use-case seems to be missing - it’s possible to make it work but requires some manual labor, e.g. it’s not guaranteed ch.changes.config will be actually set and various get_field, apply_changes etc will skip current validation info. valid?: false, as mentioned above. No issues though. Works for me.