Hi,
I’m working on a form and I’m not sure how to handle a specific type of input so I’m looking for advices & best practices.
Here’s how it works : the (text) input has an optional numeric value attached to it (we can think of an article that may have an additional cost in a specific context).
The UI is supposed to look like this :
- first article : no additional field
- second article : additional field set to 14
- third article is being created and for now the user has just activated the additional field but not set it yet (so we’ve got something I don’t want in the db : “has_additional” true, but additional value is nil.
Here’s what I currently do :
I use a functional component for the UI with the state handled by the lv
I use an Ecto schema to model this and in order to get it to work I included in it an “has_additional” along with the value of the additional field which is a nullable number.
The problem I have is that I’m not sure how to be consistent :
- in the db : if the value is nil, the “has_additional” should be false (and the opposite)
- in the UI : the value may be nil and the “has_additional” true when the user just activated the toggle
(also, I’m not fond of having this “has_additional” field : having the value to nil or not should be enough )
The solutions I see :
- I’m thinking of using 2 different changesets : one for the validation and one for the persistence but it’s deeply buried so (in my understanding) I’d have to create a new Changeset for every schema.
- I guess I may also use a live component to encapsulate the toggle state
Not sure if I’m clear.
How would you handle this ?
Best,
Matthieu