zoedsoupe

zoedsoupe

How to properly create nested form for 1-N relations?

I followed this tutorial: Nested model forms with Phoenix LiveView · FullstackPhoenix

I have a schema called Folha, defined as:

  schema "folhas" do
    field :competencia, :string

    belongs_to :empresa, Empresa
    has_many :itens, ItensFolha

    timestamps()
  end

  @doc false
  def changeset(folha, attrs) do
    folha
    |> cast(attrs, [:competencia, :empresa_id])
    |> validate_required([:competencia, :empresa_id])
    |> parse_string(:competencia)
    |> foreign_key_constraint(:empresa_id)
    |> cast_assoc(:itens, required: true, with: &ItensFolha.changeset/2)
  end

And the ItensFolha is defined as:

  schema "itens_folha" do
    field :adicional_noturno, :decimal
    field :bonificacao, :decimal
    field :descontos, :decimal
    field :dias_faltas, :integer
    field :finalizado?, :boolean, default: false
    field :hora_extra, :decimal

    # fields to enable nested form
    field :temp_id, :string, virtual: true
    field :delete, :boolean, virtual: true

    belongs_to :folha, Folha
    belongs_to :funcionario, Funcionario

    timestamps()
  end

  @doc false
  def changeset(itens_folha, attrs) do
    itens_folha
    |> cast(attrs, @fields)
    |> validate_required(@required_fields)
    |> foreign_key_constraint(:funcionario)
    |> maybe_mark_for_deletion()
  end

  defp maybe_mark_for_deletion(%{data: %{id: nil}} = changeset), do: changeset

  defp maybe_mark_for_deletion(changeset) do
    if get_change(changeset, :delete) do
      %{changeset | action: :delete}
    else
      changeset
    end
  end

On the live view side, I have the scaffold files: index.ex, show.ex, form_component.ex and it’s templates!

My Folha form is:

<h2><%= @title %></h2>

<%= f = form_for @changeset, "#",
  id: "folha-form",
  phx_target: @myself,
  phx_change: "validate",
  phx_submit: "save" %>
  <%= label f, :competencia %>
  <%= text_input f, :competencia %>
  <%= error_tag f, :competencia %>

  <%= label f, :empresa_id %>
  <%= text_input f, :empresa_id %>
  <%= error_tag f, :empresa_id %>

  <p>Item Folha</p>    
  
  <%= inputs_for f, :itens, fn i -> %>
    <%= label i, :dias_faltas %>
    <%= number_input i, :dias_faltas %>
    <%= error_tag i, :dias_faltas %>

    <%= label i, :hora_extra %>
    <%= number_input i, :hora_extra, step: "any" %>
    <%= error_tag i, :hora_extra %>

    <%= label i, :adicional_noturno %>
    <%= number_input i, :adicional_noturno, step: "any" %>
    <%= error_tag i, :adicional_noturno %>

    <%= label i, :descontos %>
    <%= number_input i, :descontos, step: "any" %>
    <%= error_tag i, :descontos %>

    <%= label i, :finalizado? %>
    <%= checkbox i, :finalizado? %>
    <%= error_tag i, :finalizado? %>

    <%= label i, :bonificacao %>
    <%= number_input i, :bonificacao, step: "any" %>
    <%= error_tag i, :bonificacao %>

    <%= label i, :funcionario_id %>
    <%= text_input i, :funcionario_id %>
    <%= error_tag i, :funcionario_id %>

    <%= if is_nil(i.data.temp_id) do %>
      <%= checkbox i, :delete %>
    <% else %>
      <%= hidden_input i, :temp_id %>
      <a href="#" phx-click="remove-item" phx-value-remove="<%= i.data.temp_id %>">&times</a>
    <% end %>
  <% end %>

  <a href="#" phx-click="add-item">Add a item folha</a>
  
  <%= submit "Save", phx_disable_with: "Saving..." %>
</form>

However, where I put the handle_event/3 “add-item” and “remove_item” clauses?

I tried to put on form_component.ex but when I clicked the button, it throws a “validate” event…
So I tried to put on index.ex, however, the socket on this live view does not have the field changeset, so I can’t to retrieve existing items. What I’m doing wrong?

I created a gist with some code: https://gist.github.com/a0e7230f312fb50fe681b984bac9b7a6

I also have all templates and live views for the ItensFolha schema, could I “embed” one form_component into another?

First 2 of 2 Posts Switch mode

zoedsoupe

zoedsoupe OP

Also, my test fails as:

1) test Index saves new folha (ContsWeb.FolhaLiveTest)
     test/conts_web/live/folha_live_test.exs:34
     ** (ArgumentError) could not find non-disabled input, select or textarea with name "folha[itens][]" within:

         <input name="_csrf_token" type="hidden" value="ACJTGywjKjwjHhgWHSJNEToZXmsgfTo1Vo2yIMyef-oRSVusVAi8O1R_"/>
         <input id="folha-form_competencia" name="folha[competencia]" type="text" value=""/>
         <input id="folha-form_empresa_id" name="folha[empresa_id]" type="text" value=""/>

     code: |> render_submit()
     stacktrace:
       (phoenix_live_view 0.15.4) lib/phoenix_live_view/test/live_view_test.ex:885: Phoenix.LiveViewTest.call/2
       test/conts_web/live/folha_live_test.exs:52: (test)
millllllz

millllllz

I struggled with this too looking at the same tutorial! What you need is the phx-target attribute on your add and remove links. You can see more about this in the docs. Now you can add your handle_events to the form component module, which will have access to the changeset in the socket assigns i.e. socket.assigns.changeset! Something like <a href="#" phx-click="add-item" phx-target="<%= @myself %>">Add a item folha</a> should do the trick!

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement