Hello everybody and thank you in advance
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