Create a gist now

Instantly share code, notes, and snippets.

@samdvr /event.ex Secret
Created May 4, 2017

What would you like to do?
Elixir DateTime
defmodule Repro.Event do
use Repro.Web, :model
schema "events" do
field :title, :string
field :location, :string
field :date, Ecto.Date
field :description, :string
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title, :location, :date, :description])
|> validate_required([:title, :location, :date, :description])
|> validate_change(:date, &must_be_future/2)
end
defp must_be_future(_, value) do
Ecto.DateTime.compare(Ecto.DateTime.from_date(value), Ecto.DateTime.utc)
|> get_error
end
defp get_error(comparison) when comparison == :lt do
[date: "cant be in the past"]
end
defp get_error(_), do: []
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment