baxterw3b

baxterw3b

Checking if a changeset is valid and how to get specific values

Hi guys,
I think that i love elixir, and phoenix, it’s so simple to understand the flow of the app and the connection with pipelines, but, i’m hating one thing..

it’s hard for me to understand the pattern matching on a changeset, i mean i have to do the destructuring everytime to get a specific value? or can i use the “.” to access to keys and values? cause i mean, to me it’s more simple than write everytime all the struct, to match and grab a value. :slight_smile:
and to get a simple is_valid value, i have to do a pattern match on the changeset when i could get the is_valid with a key check.

To me it is like, “Don’t use the . to access cause is wrong in functional programming, instead do a pattern match to grab a value”.

thanks.

Most Liked

tyro

tyro

If you want to check that a changeset is valid in a function head you normally do it something like:

def some_func(%Ecto.Changeset{valid?: true} = changeset) do
  ...
end

as this allows you to bind a name to the changeset and also perform the validation check in a concise way. You can also bind nested vars this way. e.g.

def some_func(%Ecto.Changeset{valid?: true, changes: %{password: pass}}) do
  ..
end

If you just want to check if a changeset is valid and you are not pattern matching, then yes, you should just check changeset.valid?.

hubertlepicki

hubertlepicki

You should be able to access the properties with a dot notation. For example look here:

http://blog.danielberkompas.com/elixir/2015/05/08/testing-ecto-validations.html

def create(conn, params) do
  user = MyApp.User.changeset(%MyApp.User{}, params)

  if user.valid?
    MyApp.Repo.insert(user)
    # return success
  else
    # render error
  end
end

Do you do something similar?

baxterw3b

baxterw3b

cool man!

no, in my case on the phoenix book i bought, it does something like %User{“is_valid” => return_value} = changeset, something like that, and grab that value from the struct.

The point is, i understand that is maybe the best thing to do pattern matching, but it seem like i’m forced to use it instead of the dot syntax.

So if there is no particular reason, is ok if i use the dot syntax?

thanks.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement