Re-using changesets (in live view) and validations

Hello fellow elixirists!

In working on my hobby project in live view, I find myself trying to reuse a change set as various events happen and changes are gathered, it seems like the natural place to store those changes once rather than into a whole different scattering of assigns and have to reassemble the change set each time.

The problem I find is that validations cannot be reset without either dropping the existing potentially invalid changes into a schema, or manually setting %Changeset{old | valid: true, errors: []} has anyone else wandered in this and have a better solution?

I find myself not wanting to create a new change set each time because of the ugly-ness of having to get them back out of the assigns each time you do so just seeming like excess lines of code for no reason, especially as the assigns would not be used other than to create the change set.

I could wrap the change set in a struct and fetch it out each time / have the context work with it, but that seems like an extra abstraction when I’ve already got one that mostly works.

I could split the validations off and only run them when passing into the form or preparing a submit, but I’m not sure why I dislike that, perhaps because its scattered?

Is there scope for a reset_validations function for Ecto itself here to properly reuse the changeset?

Thoughts and opinions?
Cheers
Jon

1 Like