Sha256: eaf6e7b8e5ac03f318b9733d7f71faeca8dd47ca907634c08c0464d5cdffe331

Contents?: true

Size: 1.4 KB

Versions: 12

Compression:

Stored size: 1.4 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::Forms::FormsPreview, :caption_template_form %>

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
primer_view_components-0.1.9 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.8 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.7 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.6 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.5 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.4 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.3 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.2 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.1 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.1.0 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.0.123 previews/pages/forms/03_caption_templates.md.erb
primer_view_components-0.0.122 previews/pages/forms/03_caption_templates.md.erb