Validate ends_on being after begins_on

Closes #315

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-11-18 18:40:03 +01:00
parent 4dc4524e71
commit 634a0b851e
5 changed files with 104 additions and 26 deletions

View File

@@ -128,7 +128,6 @@ defmodule Mobilizon.Events.Event do
|> common_changeset(attrs)
|> put_creator_if_published(:create)
|> validate_required(@required_attrs)
|> validate_lengths()
end
@doc false
@@ -139,7 +138,6 @@ defmodule Mobilizon.Events.Event do
|> common_changeset(attrs)
|> put_creator_if_published(:update)
|> validate_required(@update_required_attrs)
|> validate_lengths()
end
@spec common_changeset(Changeset.t(), map) :: Changeset.t()
@@ -149,6 +147,8 @@ defmodule Mobilizon.Events.Event do
|> put_tags(attrs)
|> put_address(attrs)
|> put_picture(attrs)
|> validate_lengths()
|> validate_end_time()
end
@spec validate_lengths(Changeset.t()) :: Changeset.t()
@@ -162,6 +162,20 @@ defmodule Mobilizon.Events.Event do
|> validate_length(:slug, min: 3, max: 200)
end
defp validate_end_time(%Changeset{} = changeset) do
case fetch_field(changeset, :begins_on) do
{_, begins_on} ->
validate_change(changeset, :ends_on, fn :ends_on, ends_on ->
if begins_on > ends_on,
do: [ends_on: "ends_on cannot be set before begins_on"],
else: []
end)
:error ->
changeset
end
end
@doc """
Checks whether an event can be managed.
"""