Hi,
I’m curious if anyone has any suggestions for how to handle situations where the formatting of a field in the UI (via a LiveView client hook in JS) differs from how the data is stored in Ecto.
I’m storing a phone number in Ecto as a string of 10 digits with no formatting. I’m now trying to use the PhoneNumber
hook at https://hexdocs.pm/phoenix_live_view/js-interop.html#client-hooks to format the phone number in JS (slightly different JS code but same idea). This works fine when changing the phone number field, however when I make changes to other fields that validate server side on blur, the changeset is sent back from server to client including the phone number as a string of 10 digits, which then shows up as just 10 digits in the form.
It feels like the cleanest way to handle this will be to have the JS code intercept sends to the server (removing formatting before doing so) and intercepts receives from the server (adding formatting back). This way my Elixir code can keep functioning as it does throughout the code base.
Has anyone else dealt with an issue like this? I thought perhaps the updated
or beforeUpdate
client hooks could be used to intercept incoming server side changes, but that didn’t work and validation of other fields can still leave me in a state where the previously formatted phone number shows up as 10 digits.
Any guidance or tips are appreciated
Thanks.
Justin