Display errors for table-embedeed forms

This commit is contained in:
cryptogopher 2024-03-15 02:40:17 +01:00
parent 2c0466b51a
commit f298acd726
12 changed files with 28 additions and 14 deletions

View File

@ -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 = {})

View File

@ -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 %>

View File

@ -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 %>

View File

@ -1,2 +1,3 @@
<%= turbo_stream.close_form @unit %>
<%= turbo_stream.update :flashes %>
<%#= turbo_stream.focus link_id %>

View File

@ -1,5 +1,5 @@
<div class="main">
<%= 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) %>

View File

@ -1,5 +1,5 @@
<div class="main">
<%= 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,

View File

@ -1,5 +1,5 @@
<div class="main">
<%= 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) %>

View File

@ -9,7 +9,7 @@
</div>
<% 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,

View File

@ -1,5 +1,5 @@
<div class="main">
<%= 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",

View File

@ -1,5 +1,5 @@
<div class="main">
<%= 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" %>

View File

@ -4,7 +4,7 @@
</div>
<% 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,

View File

@ -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: