Sha256: e03ef8f3ba5f455baa87f35e5f304326c20c289d7d26f04f6ec04a677a3cd20c

Contents?: true

Size: 1.39 KB

Versions: 116

Compression:

Stored size: 1.39 KB

Contents

---
title: "Caption templates"
id: caption_templates
---

All inputs support the `caption:` argument as a string, which renders explainer text underneath the input and validation message. Caption strings can be anything that responds to `#to_s`, meaning HTML-safe strings can be used in cases that need a little bit of markup:

<%= code :ruby do %>
  class ExampleForm < ApplicationForm
    form do |example_form|
      example_form.text_field(
        name: :disco_name,
        label: "Disco name",
        caption: "Enter your <strong>grooviest</strong> name".html_safe
      )
    end
  end
<% end %>

## Defining caption templates

In cases where a lot of logic or markup is necessary, or where the caption text is awkward to express in Ruby code, forms may define caption content in separate template files. Caption templates are located in a directory named after the form. For example, if `ExampleForm` lives in app/forms/example\_form.rb, its caption templates should be created in the `app/forms/example_form/` directory.

Caption template files must be named after the field they describe. For example, a caption template for the `disco_name` field above would live in app/forms/example\_form/disco\_name_caption.html.erb.

In cases where the `caption:` argument _and_ a caption template are provided, the `caption:` argument takes precedence.

## Example

<%= embed Primer::FormsPreview, :caption_template_form %>

Version data entries

116 entries across 116 versions & 2 rubygems

Version Path
primer_view_components-0.22.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.21.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.25.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.24.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.21.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.20.1 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.20.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.23.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.19.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.22.2 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.18.2 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.22.1 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.18.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.22.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.18.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.21.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.17.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.20.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.16.1 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.16.0 previews/pages/forms/03_caption_templates.md.erb