alexbaetz

alexbaetz

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

Showing Posts 1 to 2

karolsluszniak

karolsluszniak

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.

alexbaetz

alexbaetz OP

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.

— All posts loaded —

Where Next? Top

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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews