Skip to content

Commit

Permalink
Display validation errors for file fields on album form
Browse files Browse the repository at this point in the history
Although we were already displaying the validation errors at the top of
the page, I thought it would be better to also display them against the
relevant form field.

I found the `TailwindFormBuilder` a bit hard to follow and/or customize.
Ultimately I think it would be nice to come up with a tailwind-ified
`file_field` method on the `TailwindFormBuilder`. However, the existing
callsites are already sufficiently different that doing that is a bit
of a challenge.

So the best I could come up with was to make the existing
`TailwindFormBuilder#labels` method public and call that from the form
so that we now display validation errors as part of the label if there
are any, just like we already do for text-like fields. This also means
we can rely on the styles added in the form builder rather than
duplicating them in the form.

I'm not wild about the name of `TailwindFormBuilder#labels` - I assume
it relates to the idea that there is an "error label" as well as the
actual label, but it doesn't read well to me in the form markup. Maybe
`#label_with_errors` would be better?

We might want to consider re-using this on the artist form, but the
styles were sufficiently different and there isn't currently any
validation for the profile picture, so I've left it for now.
  • Loading branch information
floehopper committed Dec 22, 2023
1 parent 383e09b commit be603c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions app/form_builders/tailwind_form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def currency_field(object_method, options = {})
labels + field_wrapper
end

def labels(object_method, label_options = {}, field_options = {})
label = tailwind_label(object_method, label_options, field_options)
error_label = error_label(object_method, field_options)

@template.content_tag('div', label + error_label, { class: 'flex flex-col items-start' })
end

private

def text_like_field(field_method, object_method, options = {})
Expand All @@ -75,13 +82,6 @@ def text_like_field(field_method, object_method, options = {})
labels + field
end

def labels(object_method, label_options, field_options)
label = tailwind_label(object_method, label_options, field_options)
error_label = error_label(object_method, field_options)

@template.content_tag('div', label + error_label, { class: 'flex flex-col items-start' })
end

def tailwind_label(object_method, label_options, field_options)
text, label_opts = if label_options.present?
[label_options[:text], label_options.except(:text)]
Expand Down
8 changes: 4 additions & 4 deletions app/views/albums/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= form.currency_field :price, symbol: '£', class: 'w-full mb-2' %>
<%= form.text_area :about, rows: 10, class: 'w-full mb-3' %>
<%= form.text_area :credits, rows: 10, class: 'w-full mb-3' %>
<%= form.label :cover %>
<%= form.labels :cover %>
<%= form.file_field :cover, accept: 'image/jpeg,image/png', class: 'block border border-slate-200 mb-3 file:mr-3 file:px-3 file:py-3 w-full file:border-0 file:bg-amber-600 hover:file:bg-amber-500 file:text-white' %>
<div class="flex items-center gap-2 mb-4">
<span class="text-amber-600">
Expand All @@ -28,9 +28,9 @@
<%= f.text_field :title, class: 'w-full mb-6', required: true %>
<% if f.object.original.attached? %>
<%= f.label :original, "File (#{f.object.original.filename})", class: 'text-slate-600 mb-1 md:mb-0 pr-4' %>
<%= f.labels :original, text: "File (#{f.object.original.filename})" %>
<% else %>
<%= f.label :original, "File", class: 'text-slate-600 mb-1 md:mb-0 pr-4' %>
<%= f.labels :original, text: "File" %>
<% end %>
<%= f.file_field :original, accept: 'audio/x-wav', direct_upload: true, class: 'block border border-slate-200 mb-2 file:mr-3 file:px-3 file:py-3 w-full file:border-0 file:bg-amber-600 hover:file:bg-amber-500 file:text-white' %>
<div class="progressContainer hidden w-full h-4 bg-slate-200">
Expand All @@ -49,7 +49,7 @@
<%= form.fields_for :tracks, Track.new, child_index: "__CHILD_INDEX__" do |f| %>
<div class="border-0 border-l-4 border pl-4 mb-6 border-slate-200" >
<%= f.text_field :title, class: 'w-full mb-6', required: true %>
<%= f.label :original, "File", class: 'text-slate-600 mb-1 md:mb-0 pr-4' %>
<%= f.labels :original, text: "File" %>
<%= f.file_field :original, accept: 'audio/x-wav', direct_upload: true, class: 'block border border-slate-200 mb-2 file:mr-3 file:px-3 file:py-3 w-full file:border-0 file:bg-amber-600 hover:file:bg-amber-500 file:text-white' %>
<div class="progressContainer hidden w-full h-4 bg-slate-200">
<div class="progress w-0 h-4 bg-slate-500"></div>
Expand Down

0 comments on commit be603c1

Please sign in to comment.