Hello everyone!
I’m facing an issue and would love to hear your thoughts or opinions before diving deeper.
Problem
I need to generate a form dynamically based on embedded schemas. For example:
# my_custom_embedded_schema_1.ex
embedded_schema do
field :value1, :string
field :value2, :boolean
field :value3, :integer
end
# my_custom_embedded_schema_2.ex
embedded_schema do
field :other_value_1, :string
field :other_value2, :boolean
field :other_value3, :integer
end
...
Current Approach
In my LiveView, I’m pattern matching against each custom schema and manually generating the corresponding HEEx template:
# my_custos_forms.ex
def generate(my_custom_embedded_schema_1, assigns) do
~H"""
<.simple_form
for={@my_custom_embedded_schema_1_form}
phx-submit="save"
phx-target={@myself}
id="my_custom_embedded_schema_1_form"
phx-change="validate"
phx-debounce="500"
>
<div class="grid grid-cols-3 gap-4">
<.input field={@my_custom_embedded_schema_1_form[:value1]} label="value1" />
<.input field={@my_custom_embedded_schema_1_form[:value2]} label="value2" />
<.input field={@my_custom_embedded_schema_1_form[:value3]} label="value3" />
</div>
</.simple_form>
"""
end
def generate(my_custom_embedded_schema_2, assigns) do
# Similar implementation for other schemas
end
Pain Point
Every time I add or modify a schema, I have to manually update the forms. This is tedious and prone to errors.
Idea
I’m considering building a form generator that can dynamically generate the appropriate HTML based on the fields in the embedded schema. The goal is to reduce the manual effort required to update forms whenever a schema changes.
Questions
- Has anyone implemented something similar or encountered a similar use case?
- Are there libraries, best practices, or patterns in the Elixir/Phoenix ecosystem that could help streamline this process?
- Any potential pitfalls I should be aware of before investing time into building a generator like this?
I’d love to hear your insights!