BLacika
Hi!
I would like to ask for help. I have a form and I would like to change the values of some fields if I change one of them.
For example, I have this code snippet:
@impl true
def handle_event("validate", %{"_target" => ["invoice", "date"], "invoice" => invoice_params}, socket) do
%{"date" => date} = invoice_params
invoice = %{
socket.assigns.invoice
| due_date: Date.add(Date.from_iso8601!(date), socket.assigns.invoice.payment_term),
date: Date.from_iso8601!(date)
}
changeset =
invoice
|> Invoices.change_invoice(invoice_params)
|> Ecto.Changeset.put_assoc(:invoice_lines, invoice.invoice_lines)
|> Map.put(:action, :validate)
{:noreply,
socket
|> assign(:invoice, invoice)
|> assign(:changeset, changeset)}
end
I want to update the due_date field. But this does not change the field in the form.
I’ve manually updated the socket.assigns.invoice, it doesn’t work either.
I have a partner field which works though. When I select a partner, it goes through a few updates. The problem there is that the validation on the other field (date) does not work. Here is the code for that:
@impl true
def handle_event(
"validate",
%{"_target" => ["invoice", "partner_id"], "invoice" => %{"partner_id" => partner_id}} = invoice_params,
socket) when partner_id != "" do
partner = Partners.get_partner!(partner_id) |> Repo.preload([:bank_accounts])
invoice = %{
socket.assigns.invoice
| partner_id: partner.id,
payment_method: partner.payment_method,
payment_term: partner.payment_term,
currency_id: partner.currency_id,
due_date: Date.add(socket.assigns.invoice.date, partner.payment_term),
exchange_rate: exchange_rate(partner.currency_id)
}
changeset = invoice_changeset(invoice, invoice_params) |> Map.put(:action, :validate)
{:noreply,
socket
|> assign(:partner, partner)
|> assign(:invoice, invoice)
|> assign(:partner, partner)
|> assign(:changeset, changeset)
|> assign(:bank_accounts, bank_accounts(partner.bank_accounts))}
end
Is there anything in the documentation about this, or perhaps an example? I have not found anything similar here on the forum. Thank you! ![]()
Trending in Questions
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
Hmm, what does your HEEX code look like?
And as a sanity check, could you add an
IO.inspect(changeset, label: "changeset: ")and post the console output? It also might be worth settingliveSocket.enableDebug()and sharign what comes through in your browser’s developer console.trisolaran
This might be a long shot, but could it be that the element in the form whose value you want to update is the one having focus? Because in that case LiveView would never overwrite the value entered by the user.
BLacika
Hi!
When i change partner which affect other fields:
Brower console:
When i change date which doesn’t work:
Browser console:
Which may also be interesting is i don’t assign starter invoice datas in mount, but in apply action, because i use this form inside a liveview, and later i would like to use this liveview as edit mode:
BLacika
No. I’ve been watching this on purpose.
codeanpeace
Is the
invoice_changesetfunction doing something different in the working partner validate callback compared to the date validate callback? Also, what’s the reasoning behind theput_assochere?invoice.invoice_linesis coming straight from the socket assigns and not the event data.codeanpeace
Ahh, I think you may be overwriting the due date in your callback function.
The form’s current
due_dateis being sent back in the parameters nested under"invoice".This means that the
invoice_paramsfrom the callback function head includes the"due_date"coming from the form. AssumingInvoices.change_invoice/2function is pretty standard, the form’s"due_date"in theinvoice_paramswill replace thedue_datein theinvoicestruct that you calculated and defined in the function itself.BLacika
Thank you very much for your help and guidance. You helped a lot
It finally helped me to find out where I went wrong. Basically, I should have read the documentation better
The thing is, I thought that socket.assigns.invoice reflected the state of the form and I started to update it. I rewrote the code and now I update the params fields from the form and compile the changeset that way. So now it works as I had intended.
Like here:
codeanpeace
Glad to hear it helped!
Yeah, I did wonder why you were updating a db resource in the socket assigns since that usually only happens after a successful database insert/update/destroy operation. Like you’ve already figured out, it’s the changeset in the socket assigns that’s meant to reflect the state of the form.
Just curious, what’s the intention behind this line? Is this a multi stage form where the associated invoice lines don’t appear until after a valid date is selected?
BLacika
Nope. The invoice has a has_many relationship with invoice_line schema. I managed the implement a handle_event that add new invoice_lines to the invoice. And i don’t know even if this line should be here, because i thought that every time i update the changeset, i should use put_assoc as well. And this line should also be fixed, because i think here should be invoice_params[“invoice_lines”]. But am I correct that I only need to apply this if the change affects the invoice_line array?
codeanpeace
You can definitely use a changeset without
put_assocwhen you’re just working with the parent resource. If the form that’s connected to this validation event doesn’t have fields for the associations, which is suggested because that line pulls theinvoice_linesoff the socket rather than the params, I suspect that theput_assocline isn’t necessary.