From 3c6c2b45a8025a269681230137e1f26b555520af Mon Sep 17 00:00:00 2001 From: Hamish Murphy Date: Tue, 10 Dec 2024 18:01:20 +1100 Subject: [PATCH] Clean up forms V1 error trapping --- lib/petal_components/form.ex | 137 ++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 58 deletions(-) diff --git a/lib/petal_components/form.ex b/lib/petal_components/form.ex index 58477bb..6efc664 100644 --- a/lib/petal_components/form.ex +++ b/lib/petal_components/form.ex @@ -243,7 +243,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def text_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.text_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -257,7 +258,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def email_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.email_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -271,7 +273,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def number_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.number_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -285,7 +288,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def password_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.password_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -299,7 +303,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def search_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.search_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -313,7 +318,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def telephone_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.telephone_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -327,7 +333,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def url_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.url_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -341,7 +348,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def time_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.time_input(@form, @field, [class: @classes, bob: "yo"] ++ Map.to_list(@rest))} @@ -355,7 +363,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def time_select(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H"""
@@ -371,7 +380,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def datetime_local_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.datetime_local_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -385,7 +395,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def datetime_select(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H"""
@@ -401,7 +412,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def date_select(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H"""
@@ -417,7 +429,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def date_input(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.date_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -431,7 +444,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def color_input(assigns) do - assigns = assign_defaults(assigns, color_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, color_input_classes(errors)) ~H""" {Form.color_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -445,7 +459,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def file_input(assigns) do - assigns = assign_defaults(assigns, file_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, file_input_classes(errors)) ~H""" {Form.file_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -459,7 +474,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def range_input(assigns) do - assigns = assign_defaults(assigns, range_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, range_input_classes(errors)) ~H""" {Form.range_input(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -473,7 +489,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def textarea(assigns) do - assigns = assign_defaults(assigns, text_input_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, text_input_classes(errors)) ~H""" {Form.textarea(@form, @field, [class: @classes, rows: "4"] ++ Map.to_list(@rest))} @@ -488,7 +505,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def select(assigns) do - assigns = assign_defaults(assigns, select_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, select_classes(errors)) ~H""" {Form.select(@form, @field, @options, [class: @classes] ++ Map.to_list(@rest))} @@ -505,7 +523,8 @@ defmodule PetalComponents.Form do ) def checkbox(assigns) do - assigns = assign_defaults(assigns, checkbox_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, checkbox_classes(errors)) ~H""" {Form.checkbox(@form, @field, [class: @classes] ++ Map.to_list(@rest))} @@ -522,9 +541,11 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @form_attrs) def checkbox_group(assigns) do + errors = used_input_errors(assigns) + assigns = assigns - |> assign_defaults(checkbox_classes(field_has_errors?(assigns))) + |> assign_defaults(checkbox_classes(errors)) |> assign_new(:checked, fn -> values = case Phoenix.HTML.Form.input_value(assigns[:form], assigns[:field]) do @@ -576,7 +597,8 @@ defmodule PetalComponents.Form do attr(:rest, :global, include: @checkbox_form_attrs) def switch(assigns) do - assigns = assign_defaults(assigns, switch_classes(field_has_errors?(assigns))) + errors = used_input_errors(assigns) + assigns = assign_defaults(assigns, switch_classes(errors)) ~H"""