From 0ced9df13ad2dca04360fdd07a6f8a82e41191fc Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 31 Aug 2019 15:13:29 -0400 Subject: [PATCH] don't allow empty group titles --- .../controllers/group_controller.ex | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/frenzy_web/controllers/group_controller.ex b/lib/frenzy_web/controllers/group_controller.ex index 2a71237..337c121 100644 --- a/lib/frenzy_web/controllers/group_controller.ex +++ b/lib/frenzy_web/controllers/group_controller.ex @@ -54,15 +54,22 @@ defmodule FrenzyWeb.GroupController do def create(conn, %{"group" => %{"title" => title}}) do user = conn.assigns[:user] + title = title |> String.trim() - changeset = - Ecto.build_assoc(user, :groups, %{ - title: title - }) + if String.length(title) > 0 do + changeset = + Ecto.build_assoc(user, :groups, %{ + title: title + }) - {:ok, group} = Repo.insert(changeset) + {:ok, group} = Repo.insert(changeset) - redirect(conn, to: Routes.group_path(Endpoint, :show, group.id)) + redirect(conn, to: Routes.group_path(Endpoint, :show, group.id)) + else + conn + |> put_flash(:error, "Group title must not be empty") + |> redirect(to: Routes.group_path(Endpoint, :new)) + end end def delete(conn, _params) do