From f298acd7269063cdbdbea113a0c33e2f744dd484 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 15 Mar 2024 02:40:17 +0100 Subject: [PATCH] Display errors for table-embedeed forms --- app/helpers/application_helper.rb | 15 ++++++++++----- app/views/layouts/application.turbo_stream.erb | 4 +++- app/views/units/_form.html.erb | 2 +- app/views/units/_form_close.html.erb | 1 + app/views/users/confirmations/new.html.erb | 2 +- app/views/users/passwords/edit.html.erb | 2 +- app/views/users/passwords/new.html.erb | 2 +- app/views/users/registrations/edit.html.erb | 2 +- app/views/users/registrations/new.html.erb | 2 +- app/views/users/sessions/new.html.erb | 2 +- app/views/users/show.html.erb | 2 +- config/locales/en.yml | 6 ++++++ 12 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4904528..18e133c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,6 @@ module ApplicationHelper # TODO: replace legacy content_tag with tag.tagname - class TabularFormBuilder < ActionView::Helpers::FormBuilder + class LabelledFormBuilder < ActionView::Helpers::FormBuilder (field_helpers - [:label]).each do |selector| class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{selector}(method, options = {}) @@ -19,7 +19,7 @@ module ApplicationHelper end end - def table_form_for(&block) + def form_for(&block) @template.content_tag(:table, class: "centered") { yield(self) } + # Display leftover error messages (there shouldn't be any) @template.content_tag(:div, @object&.errors.full_messages.join(@template.tag :br)) @@ -50,9 +50,14 @@ module ApplicationHelper end end - def tabular_form_for(record, options = {}, &block) - options.merge! builder: TabularFormBuilder - form_for(record, **options, &-> (f) { f.table_form_for(&block) }) + def labelled_form_for(record, options = {}, &block) + options.merge! builder: LabelledFormBuilder + form_for(record, **options) { |f| f.form_for(&block) } + end + + def tabular_fields_for(record_name, record_object = nil, options = {}, &block) + flash.now[:alert] = record_name.errors.full_messages unless record_name.errors.empty? + fields_for(record_name, record_object, **options, &block) end def svg_tag(source, options = {}) diff --git a/app/views/layouts/application.turbo_stream.erb b/app/views/layouts/application.turbo_stream.erb index 3c080af..9ba0c29 100644 --- a/app/views/layouts/application.turbo_stream.erb +++ b/app/views/layouts/application.turbo_stream.erb @@ -1,4 +1,6 @@ +<%= yield %> + +<%# Some views may convert ActiveRecord errors to flashes, render at the end. %> <%= turbo_stream.update :flashes do %> <%= render_flash_messages %> <% end %> -<%= yield %> diff --git a/app/views/units/_form.html.erb b/app/views/units/_form.html.erb index dce2f88..31d3d29 100644 --- a/app/views/units/_form.html.erb +++ b/app/views/units/_form.html.erb @@ -1,4 +1,4 @@ -<%= fields_for @unit do |form| %> +<%= tabular_fields_for @unit do |form| %> <%= tag.tr id: dom_id(@unit), class: "form", onkeydown: "processKey(event)", data: {link_id: link_id} do %> diff --git a/app/views/units/_form_close.html.erb b/app/views/units/_form_close.html.erb index 3a9f2dd..e11818c 100644 --- a/app/views/units/_form_close.html.erb +++ b/app/views/units/_form_close.html.erb @@ -1,2 +1,3 @@ <%= turbo_stream.close_form @unit %> +<%= turbo_stream.update :flashes %> <%#= turbo_stream.focus link_id %> diff --git a/app/views/users/confirmations/new.html.erb b/app/views/users/confirmations/new.html.erb index e05affe..be665d6 100644 --- a/app/views/users/confirmations/new.html.erb +++ b/app/views/users/confirmations/new.html.erb @@ -1,5 +1,5 @@
- <%= tabular_form_for resource, url: user_confirmation_path do |f| %> + <%= labelled_form_for resource, url: user_confirmation_path do |f| %> <%= f.email_field :email, required: true, size: 30, autofocus: true, autocomplete: "email", value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %> diff --git a/app/views/users/passwords/edit.html.erb b/app/views/users/passwords/edit.html.erb index 9a272d4..8853076 100644 --- a/app/views/users/passwords/edit.html.erb +++ b/app/views/users/passwords/edit.html.erb @@ -1,5 +1,5 @@
- <%= tabular_form_for resource, url: user_password_path, html: {method: :put} do |f| %> + <%= labelled_form_for resource, url: user_password_path, html: {method: :put} do |f| %> <%= f.hidden_field :reset_password_token, label: false %> <%= f.password_field :password, label: t(".new_password"), required: true, size: 30, diff --git a/app/views/users/passwords/new.html.erb b/app/views/users/passwords/new.html.erb index fbd3d05..62dd179 100644 --- a/app/views/users/passwords/new.html.erb +++ b/app/views/users/passwords/new.html.erb @@ -1,5 +1,5 @@
- <%= tabular_form_for resource, url: user_password_path do |f| %> + <%= labelled_form_for resource, url: user_password_path do |f| %> <%= f.email_field :email, required: true, size: 30, autofocus: true, autocomplete: "email" %> <%= f.submit t(:recover_password) %> diff --git a/app/views/users/registrations/edit.html.erb b/app/views/users/registrations/edit.html.erb index 8e73026..d927d21 100644 --- a/app/views/users/registrations/edit.html.erb +++ b/app/views/users/registrations/edit.html.erb @@ -9,7 +9,7 @@
<% end %> -<%= tabular_form_for resource, url: registration_path(resource), html: {method: :patch} do |f| %> +<%= labelled_form_for resource, url: registration_path(resource), html: {method: :patch} do |f| %> <%= f.email_field :email, size: 30, autofocus: true, autocomplete: "off" %> <% if f.object.pending_reconfirmation? %> <%= f.text_field :unconfirmed_email, readonly: true, tabindex: -1, diff --git a/app/views/users/registrations/new.html.erb b/app/views/users/registrations/new.html.erb index d2e1e1a..1832859 100644 --- a/app/views/users/registrations/new.html.erb +++ b/app/views/users/registrations/new.html.erb @@ -1,5 +1,5 @@
- <%= tabular_form_for resource, url: user_registration_path do |f| %> + <%= labelled_form_for resource, url: user_registration_path do |f| %> <%= f.email_field :email, required: true, size: 30, autofocus: true, autocomplete: "email" %> <%= f.password_field :password, required: true, size: 30, minlength: @minimum_password_length, autocomplete: "new-password", diff --git a/app/views/users/sessions/new.html.erb b/app/views/users/sessions/new.html.erb index e476fc4..c8b9e78 100644 --- a/app/views/users/sessions/new.html.erb +++ b/app/views/users/sessions/new.html.erb @@ -1,5 +1,5 @@
- <%= tabular_form_for resource, url: user_session_path do |f| %> + <%= labelled_form_for resource, url: user_session_path do |f| %> <%= f.email_field :email, required: true, size: 30, autofocus: true, autocomplete: "email" %> <%= f.password_field :password, required: true, size: 30, minlength: @minimum_password_length, autocomplete: "current-password" %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 6f016bb..d06452c 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -4,7 +4,7 @@
<% end %> -<%= tabular_form_for @user do |f| %> +<%= labelled_form_for @user do |f| %> <%= f.email_field :email, readonly: true %> <% if f.object.pending_reconfirmation? %> <%= f.email_field :unconfirmed_email, readonly: true, diff --git a/config/locales/en.yml b/config/locales/en.yml index c44f31e..a2dd6a6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,6 +13,12 @@ en: created_at: registered confirmed_at: confirmed unconfirmed_email: Awaiting confirmation for + errors: + models: + unit: + attributes: + symbol: + taken: has to be unique actioncontroller: exceptions: status: