I am trying to update a changeset from an invalid set of changes to a valid set, but the errors do not reflect the change. The following is what I am experiencing, but not what I expected.
I would like to have the errors recalculated after updating the changeset. Is this possible? Am I using this changeset in an unsupported manner? I have also tried both Ecto.Changeset.put_change/3 and Ecto.Changeset.update_change/3. Any suggestions are appreciated.
How should this be handled, if there were the possibility to update errors? Especially imagining both validations actually being abstracted in multiple different purpose functions. It would take a great amount of validation specific logic to accurately update those errors, which doesn’t at all translate well to custom validations a user creates. Also users would need to know those update rules as well, to prevent any strange surprises.
This is an old topic by I comes here after a few searches.
I solved this problem using the changeset with an empty struct and updating the attrs with put_change. Following @Cmeurer example will be something like this:
def changeset(attrs) do
# Always use an empty struct
%__MODULE__{}
|> cast(attrs, [:top_value, :bottom_value])
|> validate_required([:top_value, :bottom_value])
|> validate_number(:top_value, greater_than: 0)
|> validate_number(:bottom_value, greater_than: 0)
end
cs = Foo.changeset(%{top_value: 10})
# Here has an error
# In my case this put_change is done in another function
cs = Ecto.Changeset.put_change(cs, :bottom_value, 10)
# Here added bottom_value, changeset still has an error
cs = Foo.changeset(cs.changes)
# Here errors are gone and valid? == true