MaratSH
Ecto schema and form's masked input correlation
Hello! I have a form where user inputs a phone (and other fields). I attach phx-hook to this input only, so it adds some masking for phone. Next, I have an ecto scheme and changeset to validate this input. When user inputs it masked, so validation handle_event function gets it like “+1 234 567 89 00” from params, but in ecto changeset i need to validate: is not empty (so only digits string should exist), starts from some number, the length should be i range and etc. So i create a new field where i delete all non digit symbols from incoming string, and validate this string for all i need. So, if i change input before pass it to changeset, it applies errors in “to_form(changeset)”, but it also changes input value. In other case, I create a new field in changeset, cast and validate it, so initial attrs didn’t change so form value didn’t change too. But it’s hard to relate validation errors from changeset to apply it in UI. How do you solve a problem like this? Did I miss a ready-made solution?
Most Liked
woylie
If I understand your problem correctly, you want to validate the phone number and normalize the entered value when it is actually written to the database, is that correct? If so, you might want to have a look at Ecto.Changeset.prepare_changes/2.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









