From 86c0fb19f51c13f0861279398ad3341060ca0697 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Fri, 15 Sep 2023 11:49:45 -0400 Subject: [PATCH] Validate layout/page templates on context actions Instead of validating it on every form change --- lib/beacon/content.ex | 4 ++++ lib/beacon/content/layout.ex | 2 +- lib/beacon/content/page.ex | 2 -- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/beacon/content.ex b/lib/beacon/content.ex index 5851e320..ed594225 100644 --- a/lib/beacon/content.ex +++ b/lib/beacon/content.ex @@ -1872,6 +1872,10 @@ defmodule Beacon.Content do ## Utils + defp do_validate_template(changeset, field, _format, nil = _template, _metadata) do + Changeset.add_error(changeset, field, "can't be blank", compilation_error: nil) + end + defp do_validate_template(changeset, field, :heex = _format, template, metadata) when is_binary(template) do Changeset.validate_change(changeset, field, fn ^field, template -> case Beacon.Template.HEEx.compile(template, metadata) do diff --git a/lib/beacon/content/layout.ex b/lib/beacon/content/layout.ex index 858037a3..9bcedca6 100644 --- a/lib/beacon/content/layout.ex +++ b/lib/beacon/content/layout.ex @@ -47,6 +47,6 @@ defmodule Beacon.Content.Layout do def changeset(%__MODULE__{} = layout, attrs) do layout |> cast(attrs, [:site, :title, :template, :meta_tags, :resource_links]) - |> validate_required([:site, :title, :template]) + |> validate_required([:site, :title]) end end diff --git a/lib/beacon/content/page.ex b/lib/beacon/content/page.ex index 82cf6dae..3bba2379 100644 --- a/lib/beacon/content/page.ex +++ b/lib/beacon/content/page.ex @@ -82,7 +82,6 @@ defmodule Beacon.Content.Page do |> unique_constraint([:path, :site]) |> validate_required([ :site, - :template, :layout_id, :format ]) @@ -119,7 +118,6 @@ defmodule Beacon.Content.Page do |> unique_constraint([:path, :site]) |> validate_required([ :site, - :template, :layout_id, :format ])