Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecto changesets #26

Closed
trestrantham opened this Issue Mar 4, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@trestrantham
Copy link
Contributor

trestrantham commented Mar 4, 2015

In an effort to further look into both parameter sanitization at the controller level and validations at the model level, I had some discussions with José about changesets and they seem to be the way forward. His original proposal is outlined here and it has since been implemented into ecto (master). This is coupled with phoenix master as well which will be released as 0.10 this weekend (per chrismccord).

My proposal is to created Addict.User that is useed in a developer-specified user model that adds the various changeset functions (see José's proposal) we need to validate parameters in AddictController. These changeset functions will also provide direct tie in with validations for both predefined and custom validations as well. Two birds; one stone. 😄

Documentation for ecto changesets can be found here.

Any thoughts on this approach?

@trestrantham

This comment has been minimized.

Copy link
Contributor Author

trestrantham commented Mar 4, 2015

From José's proposal, here is an example of model and controller implementation of changesets:

Controller:

plug :scrub_params, "user" when action in [:create, :update]
plug :action

def new(conn, %{"id" => id}) do
  changeset = User.changeset %User{}
  render conn, "new.html", changeset: changeset
end

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

  if changeset.valid? do
    user = Repo.insert(changeset)
    redirect conn, to: user_path(conn, :index)
  else
    render conn, "new.html", changeset: changeset
  end
end

Model:

defmodule User do
  use Ecto.Model

  schema "users" do
    field :name
    field :email
    field :age, :integer
  end

  def changeset(user, params \\ nil) do
    user
    |> cast(params, ~w(name email), ~w(age))
    |> validate_format(:email, ~r/@/)
    |> validate_number(:age, more_than: 18)
    |> validate_unique(:email)
  end
end
@trenpixster

This comment has been minimized.

Copy link
Owner

trenpixster commented Mar 16, 2015

Love it! 👍

@kball

This comment has been minimized.

Copy link

kball commented Sep 9, 2015

I would very much like this as well, as I'm trying to add some additional validation & transformation on a registration prior to passing into Addict (e.g. a password validation field, combining username and email, etc).

@trestrantham have you made any forward progress on this approach? If not, mind if I take a look at doing it?

@trenpixster

This comment has been minimized.

Copy link
Owner

trenpixster commented Apr 7, 2016

#68

@trenpixster trenpixster closed this Apr 7, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.