I have this cliente
schema:
schema "clientes" do
field :nome, :string
field :telefone, :string
field :possui_empresa?, :boolean, default: false
belongs_to :user, User
has_one :empresa, Empresa
timestamps()
end
@doc false
def changeset(cliente, attrs) do
cliente
|> cast(attrs, [:possui_empresa?, :nome, :telefone])
|> validate_required([:possui_empresa?, :nome, :telefone])
|> parse_string(:nome)
|> validate_phone(:telefone)
|> cast_assoc(:user, required: true, with: &User.cliente_changeset/2)
end
end
and this is the user
schema:
schema "users" do
¦ field :email, :string
¦ field :password, :string, virtual: true
¦ field :password_confirmation, :string, virtual: true
¦ field :hashed_password, :string
¦ field :role, Ecto.Enum, values: @roles, default: :visitante
¦ field :confirmed_at, :naive_datetime
¦ has_one :admin, Admin
¦ has_one :cliente, Cliente
¦ has_one :contador, Contador
¦ has_one :colaborador, Colaborador
¦ timestamps()
end
this is my input_helpers
:
defmodule Conts.InputHelpers do
@moduledoc """
Define <%= input f, :pass, type: :password_input %> syntax
to create dynamic form inputs
"""
use Phoenix.HTML
alias Phoenix.HTML.Form, as: PhxForm
import ContsWeb.ErrorHelpers, only: [error_tag: 2]
@label_opts "basic-form-label"
@input_opts "basic-form-input"
def input(form, field, opts \\ []) do
label_text = opts[:label] || humanize(field)
type = opts[:type] || PhxForm.input_type(form, field)
additional_classes = opts[:class] || ""
label_opts = [class: @label_opts]
input_opts = [
class: "#{@input_opts} #{state_class(form, field)} #{additional_classes}",
id: opts[:id] || ""
]
label_opts = if opts[:id], do: [for: opts[:for]] ++ label_opts, else: label_opts
content_tag :fieldset do
label = label(form, field, label_text, label_opts)
input = input(type, form, field, input_opts)
error = error_tag(form, field)
error = if Enum.empty?(error), do: "", else: error
[label, input, error]
end
end
defp state_class(form, field) do
cond do
# The form was not yet submitted
!form.action ->
""
form.errors[field] ->
"border-red-500"
true ->
IO.inspect(form)
"border-blue-500"
end
end
# Implement clauses below for custom inputs.
# defp input(:datepicker, form, field, input_opts) do
# raise "not yet implemented"
# end
defp input(:password_confirmation, form, field, input_opts) do
apply(PhxForm, :password, [form, field, input_opts])
end
defp input(type, form, field, input_opts) do
apply(PhxForm, type, [form, field, input_opts])
end
end
and this is my current cliente
form:
[...]
<%= input f, :nome, label: "Nome completo", phx_debounce: "blur" %>
<%= input f, :telefone, label: "NĂşmero de telefone", phx_debounce: "blur", class: "mobile-masked" %>
<%= inputs_for f, :user, fn u -> %>
<%= input u, :email, phx_debounce: "blur" %>
<%= input u, :password, label: "Senha", phx_debounce: "blur" %>
<%= input u, :password_confirmation, label: "Confirme a Senha", phx_debounce: "blur" %>
<% end %>
<!-- Form Inputs End -->
<%= submit "Cadastrar", class: "basic-form-submit", disabled: !@changeset.valid?, phx_disable_with: "Cadastrando..." %>
[...]
However, users’s inputs, aka cliente[user]
does not have those style classes (border-blue-500
or border-red-500
)
What I can do to these “nested” inputs act like those that are out?
screenshot: