MatijaL

MatijaL

Need help creating a changeset function with multiple fields check both from database and changeset

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

First Post!

joey_the_snake

joey_the_snake

validate_required would tell if you all the required fields are present or not

Last Post!

al2o3cr

al2o3cr

There’s a specific function to do this - Ecto.Changeset.get_field/3

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement