MatijaL
Hi,
I’m trying to create a function for the last couple of hours with no luck so I could really use your help.
So, it’s a Phoenix app and I have a user profile form. There are several fields in that form like name, city, country, etc and when the user fills all the fields, I want to do some action. The problem I have is that some of those fields may be already filled by a user in the past, for example; user can enter a name one time and then add city and country at some other time. That means that I have to check both the existing user profile and the changeset to see if all the fields are filled. When all the fields are filled, wether in profile or in the changeset, I want it to do some action.
I have really hard time checking for all those things, there are quite a lot of conditionals here so I could really use some guidance on how to create a function that would do all those checks. I’m still a beginner and this is probably simple thing but I’m really struggling at the moment.
def changeset(%Profile{} = profile, attrs \\ %{}) do
profile
|> cast(attrs, [:name, :bio, :city, :state, :country])
|> maybe_complete_profile(profile)
end
defp maybe_complete_profile(changeset, profile) do
# MISSING LOGIC
# if all the fields, in profile or in the changeset are filled do something
# else return changeset
end
Trending in Questions
Other Trending 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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 2 of 2 Posts
joey_the_snake
validate_required would tell if you all the required fields are present or not
al2o3cr
There’s a specific function to do this -
Ecto.Changeset.get_field/3