Hey,
I have a nested form
<%= f = form_for @config_changeset, "#", [phx_submit: :save_config, as: :config] %>
<%= if @config_changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= Input.text f, :heading, label: "Content Heading" %>
<hr/>
<%= inputs_for f, :sections, fn fp -> %>
<%= Input.text fp, :heading, label: "Section Heading" %>
<%= Input.textarea fp, :body, label: "Section Body"%>
<button class="btn btn-link" phx-click="project_information_remove_section" ><i class="mdi mdi-trash-can-outline"></i> Remove Section</button>
<hr/>
<% end %>
<div>
<button class="btn btn-outline-secondary" phx-click="project_information_add_section" >
<i class="mdi mdi-plus"></i> Add Section
</button>
<%= submit "Save", class: "btn btn-primary float-right" %>
</div>
</form>
I have and add sections and a remove section button.
Add section should add a new empty section and remove should remove the selected one.
Here is the original mount:
def mount(%{"project_id" => project_id, "project_widget_id" => project_widget_id}, socket) do
project = Projects.get_project!(project_id)
project_widget = Widgets.get_project_widget!(project_widget_id, preload: [:widget])
title_changeset = Widgets.change_project_widget(project_widget)
config_changeset = project_widget.config.__struct__.changeset(project_widget.config, %{})
widget_descriptor = Widgets.widget_descriptor_from_project_widget(project_widget)
{:ok,
socket
|> assign(
project: project,
project_widget: project_widget,
title_changeset: title_changeset,
config_changeset: config_changeset,
form_name: project_widget.widget.config_type,
widget_view_module: widget_descriptor.implementation.view_module
)}
end
I think I am missing something in the logic, here is my remove handler:
I know this would remove all now, that is not the problem
def handle_event("project_information_remove_section", params, socket) do
IO.puts("remove")
project_widget = socket.assigns.project_widget
config_changeset =
project_widget.config.__struct__.changeset(project_widget.config, %{
sections: []
})
{:noreply,
assign(socket,
config_changeset: config_changeset
)}
end
What I am trying to do now, is that I create a new changeset just without sections, so I would expect that once I pass that the view would update to having no sections at all, but nothing happens.
What I am doing wrong?
I tried to update the changeset from the socket as well directly, but no luck, although i think that is the way to go to track multiple changes
This is probably more like how it should be, still no effect though:
def handle_event("project_information_remove_section", params, socket) do
config_changeset = socket.assigns.config_changeset
config_changeset =
Ecto.Changeset.change(config_changeset, %{sections: []})
|> Map.put(:action, :update)
{:noreply,
assign(socket,
config_changeset: config_changeset
)}
end