Hi all, I wanted to know if it was possible to call nested has_one structures inside a simple_form of core components, I explain:
%Account{
email: "val"
password: "val"
%Individual{
first_name: "val"
last_name: "val"
}
}
now using cast_assoc based on this guide : cast_assoc
I would build my simple_form in the following way
<.simple_form
for={@account_form}
id="account-form"
phx-submit="save_account"
phx-change="validate_account"
>
<.input
field={@account_form[:email]}
name="email"
id="email"
type="text"
label="Email"
required
/>
<.input
field={@account_form[:password]}
name="password"
id="password"
type="password"
label="Password"
required
/>
#How to access the properties of the structure
#%Individual{first_name: "val", last_name: "val"} that is inside #%Account{...%Individual{...}} based on the cast_assoc example in the link above?
<.input
field={@account_form[:individual].first_name}
name="individual_first_name"
id="individual_first_name"
type="text"
label="First name"
required
/>
</.simple_form>
In the mount function of :live_view file the code looks like this:
def mount(_params, _session, socket) do
account = %Account{ individual: %Individual{}}
changeset = Accounts.change_account_registration(account)
socket =
socket
|> assign(trigger_submit: false, check_errors: false)
|> assign_form(changeset)
{:ok, socket, temporary_assigns: [form: nil]}
end
Don’t forget that everything starts from the has_one relationship between accounts and individuals in the database.
Thank you in advance for your help