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
openproject-primer_view_components-0.43.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.43.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.32.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.42.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.31.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.41.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.41.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.40.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.30.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.39.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.39.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.38.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.29.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.28.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.37.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.37.0 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.36.2 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.36.1 previews/pages/forms/03_caption_templates.md.erb
openproject-primer_view_components-0.36.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.27.0 previews/pages/forms/03_caption_templates.md.erb