Strange behaviour of phx-no-feedback on nested form

Hello everybody and thank you in advance :slight_smile:

I’m fairly new to Phoenix/Elixir and I’m currently working on a small project to establish interactive user registration with Phoenix LiveView.
So far I’ve created the following user schema:

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "users" do
    field :password, :string
    field :user_role, :string
    field :username, :string

    has_one :admin, Admin
    has_one :employee, Employee

    timestamps()
  end

A user role can be an admin or an employee. For both user roles I’ve created another schema with the associated attributes. I’ve also set up associations between user and admin/employee by adding has_one and respectively belongs_to, where the foreign key of user will be used as the primary key of employee/admin.

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "employees" do
    belongs_to :user, User, primary_key: true

    field :first_name, :string
    field :last_name, :string
    field :department, :string
    field :begin_working_time, :time
    field :status, :string

    timestamps()
  end
  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "admins" do
    belongs_to :user, User, primary_key: true

    field :first_name, :string
    field :last_name, :time
    field :status, :string

    timestamps()
  end

Then I used cast_assoc in the changeset to validate the user_changeset as well as the respective user_role_changeset:

def validate_registration_params(user, attrs) do
    changeset = user
    |> cast(attrs, [:username, :password, :user_role])
    |> validate_required([:username, :password, :user_role])
    |> validate_confirmation(:password, required: true)
    |> unique_constraint(:username)

    changeset =
      case attrs["user_role"] do
        "employee" ->
          changeset
          |> cast_assoc(:employee, with: &Employee.validate_employee/2)
        "admin" ->
          |> cast_assoc(:admin, with: &Employee.validate_employee/2)
        _ -> 
          changeset
      end

    changeset
  end

To create a new employee or admin I try to use one form which changes its input fields depending on the selected user_role in a select_tag.

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

<%= f = form_for @changeset, "#",
  id: "user-form",
  phx_change: "validate",
  phx_submit: "save" %>

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

  <%= label f, :password %>
  <%= password_input f, :password, value: input_value(f, :password) %>
  <%= error_tag f, :password %>

  <%= label f, :password_confirmation %>
  <%= password_input f, :password_confirmation, value: input_value(f, :password_confirmation) %>
  <%= error_tag f, :password_confirmation %>

  <%= label f, :user_role %>
  <%= select f, :user_role,  ["Admin": "admin", "Employee": "employee"], prompt: "Choose your role"%>
  <%= error_tag f, :user_role %>

  <%= case @user_role do %>
    <% "admin" -> %>
      <%= inputs_for f, :admin, fn fp -> %>
        <%= label fp, :first_name %>
        <%= text_input fp, :first_name %>
        <%= error_tag fp, :first_name %>

        <%= label fp, :last_name %>
        <%= text_input fp, :last_name %>
        <%= error_tag fp, :last_name %>
        
        <%= label fp, :status %>
        <%= text_input fp, :status %>
        <%= error_tag fp, :status %>
      <% end %>

  <% "employee" -> %>
    <%= inputs_for f, :employee, fn fp -> %>
      <table>
        <tr>
          <td>
          <%= label fp, :first_name, phx_focus: "myblur" %>
          <%= text_input fp, :first_name %>
          <%= error_tag fp, :first_name %>
          </td>
          <td>
          <%= label fp, :last_name %>
          <%= text_input fp, :last_name %>
          <%= error_tag fp, :last_name %>
          </td>
        </tr> 
        <tr>
          <td>
          <%= label fp, :status %>
          <%= text_input fp, :status %>
          <%= error_tag fp, :status %>
          </td>
          <td>
          <%= label fp, :department %>
          <%= text_input fp, :department %>
          <%= error_tag fp, :department %>
          </td>
        </tr>
        <tr>
          <td>
            <%= label fp, :begin_working_time %>
            <%= text_input fp, :begin_working_time %>
            <%= error_tag fp, :begin_working_time %>
          </td>
        </tr>
      </table>
    <% end %>

  <% _ -> %>
  <% end %>

  <%= submit "Save", phx_disable_with: "Saving..." %>
</form>

The strange behavior occurs as soon as I try to validate the changeset while the user inserts something, e.g. the username, and then deletes it to force the “can’t be blank” error. As soon as this error shows up all of the errors for the chosen user role are also shown, even if the fields weren’t touched so far.

I think it has something to do with the way the error_helpers assign the “phx-no-feedback”. However I can’t figure out what’s going on in detail. Especially since it does not show the error messages if I remove all of the table tags or the conditional rendering of the table or both.

Any help is much appreciated.

Kind regards,
Alex

2 Likes

Same here. The difference is that I’m using the recommended variant of inputs_for using for instead of a callback function. And I think so should you.

In my case, the problem happens after first render of inputs_for and then gets fixed once you touch the form again and force LV to retry adding the phx-no-feedback class. It seems to be some kind of LiveView JS client’s lifecycle problem - it looks like the input referenced in phx-feedback-for doesn’t exist upon the first attempt to add phx-no-feedback.

1 Like

Thank you for your advice on the inputs_for function and how to use it properly! :slight_smile:

The problem you are referencing is exactly the one I face.
The only way I get it to work already after the first render is to either exclude the <table>, especially the <td>, or to remove the conditional loading from the form. I also tried it again yesterday, but I can’t get my head around that behavior not can I find a working solution. I think until this works as expected I will stick to two separate pages which include the user roles associated input fields.