Module: Koi::Form::GOVUKExtensions

Defined in:
lib/koi/form/govuk_extensions.rb

Instance Method Summary collapse

Instance Method Details

#govuk_document_field(attribute_name, label: {}, caption: {}, hint: {}, form_group: {}, mime_types: Koi.config.document_mime_types) ⇒ Object

Generates a div element with an input with type=file with a label, optional hint.

Examples:

A upload field with label as a proc

= f.govuk_document_field :data, label: -> { tag.h3('Upload your document') }


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/koi/form/govuk_extensions.rb', line 11

def govuk_document_field(attribute_name,
                         label: {},
                         caption: {},
                         hint: {},
                         form_group: {},
                         mime_types: Koi.config.document_mime_types,
                         **,
                         &)
  Elements::Document.new(
    self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
  ).html
end

#govuk_image_field(attribute_name, label: {}, caption: {}, hint: {}, form_group: {}, mime_types: Koi.config.image_mime_types) ⇒ ActiveSupport::SafeBuffer

Generates a div element to preview uploaded images and an input with type=file with a label, optional hint

Examples:

An image field with injected content

= f.govuk_image_field :incident_image,
  label: { text: 'Attach a picture of the incident' } do

  p.govuk-inset-text
    | If you don't know exactly leave this section blank

A image upload field with label as a proc

= f.govuk_image_field :image, label: -> { tag.h3('Upload your image') }

Parameters:

  • attribute_name (Symbol)

    The name of the attribute

  • hint (Hash, Proc) (defaults to: {})

    The content of the hint. No hint will be added if ‘text’ is left nil. When a Proc is supplied the hint will be wrapped in a div instead of a span

  • label (Hash, Proc) (defaults to: {})

    configures or sets the associated label content

  • caption (Hash) (defaults to: {})

    configures or sets the caption content which is inserted above the label

  • form_group (Hash) (defaults to: {})

    configures the form group

  • & (Block)

    arbitrary HTML that will be rendered between the hint and the input

  • kwargs (Hash)

    a customizable set of options

Options Hash (label:):

  • text (String)

    the label text

  • size (String)

    the size of the label font, can be xl, l, m, s or nil

  • tag (Symbol, String)

    the label’s wrapper tag, intended to allow labels to act as page headings

  • hidden (Boolean)

    control the visibility of the label. Hidden labels will still be read by screen readers

  • kwargs (Hash)

    additional arguments are applied as attributes on the label element

Options Hash (caption:):

  • text (String)

    the caption text

  • size (String)

    the size of the caption, can be xl, l or m. Defaults to m

  • kwargs (Hash)

    additional arguments are applied as attributes on the caption span element

Options Hash (hint:):

  • text (String)

    the hint text

  • kwargs (Hash)

    additional arguments are applied as attributes to the hint

Options Hash (form_group:):

  • classes (Array, String)

    sets the form group’s classes

  • kwargs (Hash)

    additional attributes added to the form group

Returns:

  • (ActiveSupport::SafeBuffer)

    HTML output



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/koi/form/govuk_extensions.rb', line 59

def govuk_image_field(attribute_name,
                      label: {},
                      caption: {},
                      hint: {},
                      form_group: {},
                      mime_types: Koi.config.image_mime_types,
                      **,
                      &)
  Elements::Image.new(
    self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
  ).html
end