Dynamically generated forms and running out of input atoms

Hi!

It’s me again, running into a small problem about how to elegantly solve one of my issues.
The situations is this: I have a products table with a many-to-many relationship to a features table. In the UI, I am rendering a checkbox for every row in features for the user to select, and if it is ticked, that relationship is created. Now, the list of features is of course dynamic based on how many rows there are.
The code I am using for this right now is (using Surface):

<Form.Field :for={{ feature <- @features }} name={{ String.to_atom("#{feature.id}") }}>
                  <Form.Checkbox />
                  <Form.Label>{{ gettext "label" }}</Form.Label>
</Form.Field>

Now, this works. My question is about using String.to_atom/1 and DoSing myself. Using just :checkbox as the name obviously does not work as it would create multiple fields with the same name, giving me only one parameter.
Is there any way to do this without creating a possibly infinite amount of atoms? The name field does not take strings, so I am unsure how to proceed.

Thanks!

The default phoenix form helpers all work with strings. The only place of the common workflow requiring atoms are changesets, but you can use phoenix form helpers without those. Afaik this is currently a shortcoming of surface.

3 Likes

Yup, using the Phoenix helpers for now solves this. Wasn’t aware that this was Surface-specific, thanks for pointing me in the right direction!

Hi @cschmatzler!

Can you please open an issue? We should allow the field name to be a string too. If the phoenix helpers accept it, I see no reason Surface shouldn’t.

Cheers.

3 Likes

Sorry, I was out cold with a fever. I saw on GitHub that you already created one for me - thank you!

1 Like