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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews