<% # This is the main partial which all of our other field partials # invoke in `bullet_train-themes/app/views/base/fields/`. # `options`, `html_options` and `other_options` # each serve their own specific purpose, so please read the # Field Partial documentation to get more familiar with how these work. # https://bullettrain.co/docs/field-partials # # Even though not all form helpers are invoked in this partial, # all instances of `options` must be passed here because # We add the `for` attribute for labels with `options[:id]`, # and this isn't always as simple as `form.field_id(method)`. %> <% %i[label field error help after_help].each do |key| if (content = content_for(key).presence) flush_content_for key partial.section key, content end end %> <% form ||= current_fields_form # returns a struct with `label`, `placeholder`, and `help` methods. labels = labels_for(form, method) # Initialize options. options ||= {} other_options ||= {} options[:id] ||= form.field_id(method) options[:placeholder] ||= labels.placeholder if labels.placeholder # TODO: Why is this commented out, and where is `field_editable?`? # Delete this and finish https://github.com/bullet-train-co/bullet_train-core/pull/587. # options[:disabled] ||= !field_editable?(form.object, method) if user_signed_in? other_options[:help] = [other_options[:help], labels.help].compact.join(" ") if !other_options.key?(:required) other_options[:required] = options.fetch(:required) { presence_validated?(form.object, method) } end if other_options[:required] options[:"aria-required"] = true end errors = [method, method.to_s.delete_suffix("_id").to_sym].uniq.map { |attribute| form.object.errors.full_messages_for(attribute) }.flatten has_errors = errors.any? || partial.error? || other_options[:error].present? # TODO: Partials which invoke form helpers inline aren't using # these classes, although we have the `has_errors` conditional with some style below. options[:class] = "#{options[:class]} block w-full rounded-md shadow-sm font-light dark:bg-slate-800 dark:text-slate-300" options[:class] += " text-base md:text-sm" # default to 16px on mobile to prevent zooming options[:class] += " disabled:bg-slate-200 disabled:dark:bg-slate-700" # classes to give a visual indicator when a field is disabled options[:class] += if has_errors " pr-10 border-red-500 text-red-900 placeholder-red-500 outline-none focus:ring-red-500 focus:border-red-500 dark:text-slate-300" else " focus:ring-primary-500 focus:border-primary-500 border-slate-300 dark:border-slate-900" end %>
<% # the label. %>
"> <% if partial.label? %> <%= partial.label %> <% else %> <% # allow the label to be defined via an inline option or else one of the locale yaml definitions. %> <% label = (other_options[:label].presence || labels.label || legacy_label_for(form, method)) %> <%= form.label method, label&.html_safe, class: 'block', for: options[:id] %> <% end %>
<%# Here, we prioritize yielding the partial's field markup if it already exists. %> <%# `form.send` below calls the original Rails Form helpers, such as %> <%# `form.text_field(method, options)`. Refer to the form helpers for more details: %> <%# https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/form_helper.rb %>
<% # The actual field. %> <% if partial.field? %> <%= partial.field %> <% else %> <% # e.g. form.text_field(method, options) %> <%= form.send(helper, method, options) %> <% end %>
<% # any error messages. %> <% if has_errors %>

<%= errors.map { |error| error + ". " }.join %> <%= partial.error %> <% if other_options[:hide_custom_error].blank? %> <%= other_options[:error]&.html_safe %> <% end %>

<% end %> <% # any help text. %> <% if partial.help? || other_options[:help].present? || partial.after_help? %>

<%= partial.help %> <%= other_options[:help]&.html_safe %> <%= partial.after_help %>

<% end %> <% if other_options[:icon] %>
<% end %>