-
-
Save samdvr/49187575fc60c140046d20594f6593ea to your computer and use it in GitHub Desktop.
Elixir DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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