Form Helpers

August 19, 2022 ยท View on GitHub

Generated forms start you off on a good place to manage the object's content, but chances are you'll want to customize them and add more fields as you data model evolves. Fae provides a number of form helpers to help you leverage Fae's built in features will allowing customization when needed.

Form helpers in Fae use the simple_form gem as it's base. In most cases options that simple_form accepts can be passed into these helpers directly. The reason why we've established these helpers is to allow for customized options. They also provide a method to directly hook into Fae, so we can push out features and bugfixes.


Format

All form helpers are in the following format:

fae_method_name(f, attribute, options)
argumentdescription
f(required) The variable simple_form passes through in its block. The actual variable can vary based on how you setup the view, but f is the generated default.
attribute(required) The attribute the form element is displaying. It's recommended you use symbols over strings.
optionsAn optional hash of options to customize the form element.

Global Options

optiontypedefaultdescription
labelstringattribute.titleizethe form label
helper_textstringhelper text that appears under label
hintstringtext that appears in a hint modal
markdownbooleanfalseadds markdown GUI toolbar. field will support inline attachments for images, just drop image onto field to upload. use eye icon to preview image
markdown_supportedbooleanfalsedisplays support text and hint for markdown
input_classstringa class to add to the input element
wrapper_classstringa class to add to the wrapper element
validatebooleantruetriggers judge to validate element

Fae Input

fae_input

attributes only

fae_input is the basic input method derived from simple_form's f.input. What it displays will depend on the column type of the attribute. See simple_form's documentationn to learn more.

Examples

An input[type=text] with an added wrapper_class and helper_text:

fae_input f, :first_name, wrapper_class: 'special_wrapper', helper_text: 'No more than 50 characters'

A text area with Fae's built-in markdown hint:

fae_input f, :description, markdown_supported: true

(Optional) HTML Input

Fae includes the Trumbowyg HTML WYSIWYG editor. In order to reduce the size of the generated JavaScript, this libary is opt-in and needs to be loaded by including this line in the fae.js file:

// app/assets/javascripts/fae.js

//= require fae/vendor/trumbowyg

Then, the editor can be activated:

fae_input f, :description, html: true

Fae Association

fae_association

associations only

fae_association is directly derived from simple_form's f.association. Again, the element it renders depends on the association type.

optiontypedefaultdescription
collectionarray or AR:RelationAssociationClass.allan array or ActiveRecord object of items to populate the element

Examples

A dropdown listing active people

fae_association f, :people, collection: Person.active

Fae Checkbox

fae_checkbox

Checkbox

optiontypedefaultdescription
type'stacked' or 'inline'stackeddetermines how multiple checkboxes are displayed
input_type:boolean or :check_boxes:check_boxesuse :boolean for single true/false input

Examples

A single attribute checkbox to save a boolean

fae_checkbox f, :active, input_type: :boolean

Inline has_many collection of checkboxes

fae_checkbox f, :promos, type: 'inline', collection: Promo.live

Fae Radio

fae_radio

Radio

optiontypedefaultdescription
type'stacked' or 'inline'stackeddetermines how multiple checkboxes are displayed

Examples

A single attribute radio

fae_radio f, :active, type: 'inline'

A belongs_to association

fae_radio f, :wine

Fae Pulldown

fae_pulldown

Pulldown

optiontypedefaultdescription
collectionarray or AR:RelationAssociationClass.allan array or ActiveRecord object of items to populate the element
size'long' or 'short'longdetermines the size of the pulldown
searchbooleansearch is displayed with more than 10 itemsdetermines whether or not to show the search bar

Examples

A short pulldown of a belongs_to association

fae_pulldown f, :wine, size: 'short', collection: Wine.order(:name)

With search:

Pulldown with search

Fae Multiselect

fae_multiselect

Multiselect

associations only

optiontypedefaultdescription
two_panebooleanfalseBy default this will display a chosen style multiselect. Setting this to true will display the 'two pane' style.

Examples

A chosen style multiselect with custom label_method

fae_multiselect f, :acclaims, label_method: :publication

A two pane style multiselect

fae_multiselect f, :selling_points, two_pane: true

Multiselect

Fae Grouped Select

fae_grouped_select
optiontypedefaultdescription
collectionarray or AR:RelationAssociationClass.allan array or ActiveRecord object of items to populate the element
groupsarray of stringsmust be used with labels
labelsarray of stringsmust be used with groups

Fae Datepicker

fae_datepicker

Datepicker

attributes only

Examples

fae_datepicker f, :release_date

Fae Daterangepicker

fae_daterangepicker

Daterange picker

attributes only

The daterangepicker is a little different: instead of a single attribute, it accepts an array of two attributes.

Examples

fae_daterange f, [:start_date, :end_date], label: 'Start/End dates'

Fae Color Picker

The color picker includes alpha support but can be disabled by setting alpha: false

fae_color_picker f, :color

Suffix

Fae Prefix

fae_prefix

Prefix

attributes only

optiontypedefaultdescription
prefixstring(required) string will appear in prefix box
iconbooleanfalsedetermines whether or not to display prefix icon

Examples

fae_prefix f, :price, prefix: '$', placeholder: '50.00'

Fae Suffix

fae_suffix

Suffix

attributes only

optiontypedefaultdescription
suffixstring(required) string will appear in suffix box
iconbooleanfalsedetermines whether or not to display prefix icon

Examples

fae_suffix f, :weight, suffix: 'lbs'

Fae Video URL

fae_video_url

Video URL

attributes only

This helper is a normal fae_input, but provides a custom helper and hint specific to extracting YouTube IDs.

Examples

fae_video_url f, :video_url