The form
kit provides consumers with a convenient, consistently styled <form>
wrapper.
This kit uses rails form_with
with our custom builder to render forms using other kits such as text_input, select, and typeahead to name a few. Doing so provides UI consistency within forms and makes adding a form to your page easier.
<% example_collection = [ OpenStruct.new(name: "Alabama", value: 1), OpenStruct.new(name: "Alaska", value: 2), OpenStruct.new(name: "Arizona", value: 3), OpenStruct.new(name: "Arkansas", value: 4), OpenStruct.new(name: "California", value: 5), OpenStruct.new(name: "Colorado", value: 6), OpenStruct.new(name: "Connecticut", value: 7), OpenStruct.new(name: "Delaware", value: 8), OpenStruct.new(name: "Florida", value: 9), OpenStruct.new(name: "Georgia", value: 10), ] %> <% example_dropdown_options = [ { label: 'United States', value: 'United States', id: 'us' }, { label: 'Canada', value: 'Canada', id: 'ca' }, { label: 'Pakistan', value: 'Pakistan', id: 'pk' }, ] %> <%= pb_form_with(scope: :example, url: "", method: :get) do |form| %> <%= form.typeahead :example_typeahead, props: { data: { typeahead_example1: true, user: {} }, label: true, placeholder: "Search for a user" } %> <%= form.text_field :example_text_field, props: { label: true } %> <%= form.phone_number_field :example_phone_number_field, props: { label: "Example phone field" } %> <%= form.email_field :example_email_field, props: { label: true } %> <%= form.number_field :example_number_field, props: { label: true } %> <%= form.search_field :example_search_field, props: { label: true } %> <%= form.password_field :example_password_field, props: { label: true } %> <%= form.url_field :example_url_field, props: { label: true } %> <%= form.text_area :example_text_area, props: { label: true } %> <%= form.dropdown_field :example_dropdown, props: { label: true, options: example_dropdown_options } %> <%= form.select :example_select, [ ["Yes", 1], ["No", 2] ], props: { label: true } %> <%= form.collection_select :example_collection_select, example_collection, :value, :name, props: { label: true } %> <%= form.check_box :example_checkbox, data: { field1: "value1", field2: "value2" }, props: { text: "Example Checkbox", label: true }, checked_value: "yes", unchecked_value: "no", id: "checkbox-id", name: "checkbox-name", class: "checkbox-class" %> <%= form.date_picker :example_date_picker_1, props: { label: true } %> <%= form.star_rating_field :example_star_rating, props: { variant: "interactive", label: true } %> <%= form.actions do |action| %> <%= action.submit %> <%= action.button props: { type: "reset", text: "Cancel", variant: "secondary" } %> <% end %> <% end %> <!-- form.typeahead user results example template --> <template data-typeahead-example-result-option> <%= pb_rails("user", props: { name: tag(:slot, name: "name"), orientation: "horizontal", align: "left", avatar_url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGP6zwAAAgcBApocMXEAAAAASUVORK5CYII=", avatar: true }) %> </template> <!-- form.typeahead JS example implementation --> <%= javascript_tag defer: "defer" do %> document.addEventListener("pb-typeahead-kit-search", function(event) { if (!event.target.dataset || !event.target.dataset.typeaheadExample1) return; fetch(`https://api.github.com/search/users?q=${encodeURIComponent(event.detail.searchingFor)}`) .then(response => response.json()) .then((result) => { const resultOptionTemplate = document.querySelector("[data-typeahead-example-result-option]") event.detail.setResults((result.items || []).map((user) => { const wrapper = resultOptionTemplate.content.cloneNode(true) wrapper.children[0].dataset.user = JSON.stringify(user) wrapper.querySelector('slot[name="name"]').replaceWith(user.login) wrapper.querySelector('img').dataset.src = user.avatar_url return wrapper })) }) }) document.addEventListener("pb-typeahead-kit-result-option-selected", function(event) { if (!event.target.dataset.typeaheadExample1) return; const selectedUserJSON = event.detail.selected.firstElementChild.dataset.user const selectedUserData = JSON.parse(selectedUserJSON) // set the input field's value event.target.querySelector('input[name=example_typeahead]').value = selectedUserData.login // log the selected option's dataset console.log('The selected user data:') console.dir(selectedUserData) // do even more with the data later - TBD event.target.dataset.user = selectedUserJSON }) <% end %>
Validation displays an error with red border and red text below indicating that the user must fill out the field.
<% example_collection = [ OpenStruct.new(name: "Alabama", value: 1), OpenStruct.new(name: "Alaska", value: 2), OpenStruct.new(name: "Arizona", value: 3), OpenStruct.new(name: "Arkansas", value: 4), OpenStruct.new(name: "California", value: 5), OpenStruct.new(name: "Colorado", value: 6), OpenStruct.new(name: "Connecticut", value: 7), OpenStruct.new(name: "Delaware", value: 8), OpenStruct.new(name: "Florida", value: 9), OpenStruct.new(name: "Georgia", value: 10), ] %> <% example_dropdown_options = [ { label: 'United States', value: 'United States', id: 'us' }, { label: 'Canada', value: 'Canada', id: 'ca' }, { label: 'Pakistan', value: 'Pakistan', id: 'pk' }, ] %> <%= pb_form_with(scope: :example, method: :get, url: "", validate: true) do |form| %> <%= form.typeahead :example_typeahead_validation, props: { data: { typeahead_example2: true, user: {} }, label: true, placeholder: "Search for a user", required: true, validation: { message: "Please select a user." } } %> <%= form.text_field :example_text_field_validation, props: { label: true, required: true } %> <%= form.phone_number_field :example_phone_number_field_validation, props: { label: "Example phone field" } %> <%= form.email_field :example_email_field_validation, props: { label: true, required: true } %> <%= form.number_field :example_number_field_validation, props: { label: true, required: true } %> <%= form.search_field :example_project_number_validation, props: { label: true, required: true, validation: { pattern: "[0-9]{2}-[0-9]{5}", message: "Please enter a valid project number (example: 33-12345)." } } %> <%= form.password_field :example_password_field_validation, props: { label: true, required: true } %> <%= form.url_field :example_url_field_validation, props: { label: true, required: true } %> <%= form.text_area :example_text_area_validation, props: { label: true, required: true } %> <%= form.dropdown_field :example_dropdown_validation, props: { label: true, options: example_dropdown_options, required: true } %> <%= form.select :example_select_validation, [ ["Yes", 1], ["No", 2] ], props: { label: true, blank_selection: "Select One...", required: true } %> <%= form.collection_select :example_collection_select_validation, example_collection, :value, :name, props: { label: true, blank_selection: "Select One...", required: true } %> <%= form.check_box :example_checkbox, props: { text: "Example Checkbox", label: true, required: true } %> <%= form.date_picker :example_date_picker_2, props: { label: true, required: true } %> <%= form.star_rating_field :example_star_rating_validation, props: { variant: "interactive", label: true, required: true } %> <%= form.actions do |action| %> <%= action.submit %> <%= action.button props: { type: "reset", text: "Cancel", variant: "secondary" } %> <% end %> <% end %> <!-- form.typeahead user results example template --> <template data-typeahead-example-result-option> <%= pb_rails("user", props: { name: tag(:slot, name: "name"), orientation: "horizontal", align: "left", avatar_url: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGP6zwAAAgcBApocMXEAAAAASUVORK5CYII=", avatar: true }) %> </template> <!-- form.typeahead JS example implementation --> <%= javascript_tag defer: "defer" do %> document.addEventListener("pb-typeahead-kit-search", function(event) { if (!event.target.dataset || !event.target.dataset.typeaheadExample2) return fetch(`https://api.github.com/search/users?q=${encodeURIComponent(event.detail.searchingFor)}`) .then(response => response.json()) .then((result) => { const resultOptionTemplate = document.querySelector("[data-typeahead-example-result-option]") event.detail.setResults((result.items || []).map((user) => { const wrapper = resultOptionTemplate.content.cloneNode(true) wrapper.children[0].dataset.user = JSON.stringify(user) wrapper.querySelector('slot[name="name"]').replaceWith(user.login) wrapper.querySelector('img').dataset.src = user.avatar_url return wrapper })) }) }) document.addEventListener("pb-typeahead-kit-result-option-selected", function(event) { if (!event.target.dataset.typeaheadExample2) return const selectedUserJSON = event.detail.selected.firstElementChild.dataset.user const selectedUserData = JSON.parse(selectedUserJSON) // set the input field's value event.target.querySelector('input[name=example_typeahead_validation]').value = selectedUserData.login // log the selected option's dataset console.log('The selected user data:') console.dir(selectedUserData) // do even more with the data later - TBD event.target.dataset.user = selectedUserJSON }) <% end %>
Pressing Submit will trigger a loading state where the button content is replaced by a spinner icon and the submit button will be disabled.
<%= pb_form_with(scope: :example, url: "", method: :get, loading: true) do |form| %> <%= form.text_field :example_text_field_loading, props: { label: true } %> <%= form.actions do |action| %> <%= action.submit %> <%= action.button props: { type: "reset", text: "Cancel", variant: "secondary" } %> <% end %> <% end %> <script> const loadingForm = document.querySelector(".pb_form_loading") if (loadingForm) { loadingForm.addEventListener("submit", function(event) { event.preventDefault(); const submitButton = event['submitter']; const cancelButton = event['target'].querySelector('button[type="reset"]'); if (submitButton) { let currentClass = submitButton.className; let newClass = currentClass.replace("_disabled_loading", "_enabled"); let cancelClass = cancelButton ? cancelButton.className : ""; let newCancelClass = cancelClass.replace("_disabled", "_enabled"); setTimeout(function() { submitButton.disabled = false; submitButton.className = currentClass; if (cancelButton) { cancelButton.disabled = false; cancelButton.className = cancelClass; } }, 5000); } }); } </script>
Avoid using a form without validation if fields are required and using with validation if not required.
Props | Type | Values |
---|---|---|
max_width |
array
|
0%
xs
sm
md
lg
xl
xxl
0
none
100%
|
min_width |
array
|
0%
xs
sm
md
lg
xl
xxl
0
none
100%
|
width |
array
|
0%
xs
sm
md
lg
xl
xxl
0
none
100%
|
z_index |
array
|
1
2
3
4
5
6
7
8
9
10
|
number_spacing |
array
|
tabular
|
shadow |
array
|
none
deep
deeper
deepest
|
line_height |
array
|
tightest
tighter
tight
normal
loose
looser
loosest
|
display |
array
|
block
inline_block
inline
flex
inline_flex
none
|
cursor |
array
|
auto
default
none
contextMenu
help
pointer
progress
wait
cell
crosshair
text
verticalText
alias
copy
move
noDrop
notAllowed
grab
grabbing
eResize
nResize
neResize
nwResize
sResize
seResize
swResize
wResize
ewResize
nsResize
neswResize
nwseResize
colResize
rowResize
allScroll
zoomIn
zoomOut
|
flex_direction |
array
|
row
column
rowReverse
columnReverse
|
flex_wrap |
array
|
wrap
nowrap
wrapReverse
|
justify_content |
array
|
start
end
center
spaceBetween
spaceAround
spaceEvenly
|
justify_self |
array
|
auto
start
end
center
stretch
|
align_items |
array
|
flexStart
flexEnd
start
end
center
baseline
stretch
|
align_content |
array
|
start
end
center
spaceBetween
spaceAround
spaceEvenly
|
align_self |
array
|
auto
start
end
center
stretch
baseline
|
flex |
array
|
auto
initial
0
1
2
3
4
5
6
7
8
9
10
11
12
none
|
flex_grow |
array
|
1
0
|
flex_shrink |
array
|
1
0
|
order |
array
|
1
2
3
4
5
6
7
8
9
10
11
12
|
position |
array
|
relative
absolute
fixed
sticky
|
hover |
array
|
|
border_radius |
array
|
none
xs
sm
md
lg
xl
rounded
|
text_align |
array
|
start
end
left
right
center
justify
justify-all
match-parent
|
overflow |
array
|
visible
hidden
scroll
auto
|
truncate |
array
|
1
2
3
4
5
|
left |
array
|
0
xxs
xs
sm
md
lg
xl
auto
initial
inherit
|
top |
array
|
0
xxs
xs
sm
md
lg
xl
auto
initial
inherit
|
right |
array
|
0
xxs
xs
sm
md
lg
xl
auto
initial
inherit
|
bottom |
array
|
0
xxs
xs
sm
md
lg
xl
auto
initial
inherit
|
vertical_align |
array
|
baseline
super
top
middle
bottom
sub
text-top
text-bottom
|
height |
array
|
auto
xs
sm
md
lg
xl
xxl
xxxl
|
min_height |
array
|
auto
xs
sm
md
lg
xl
xxl
xxxl
|
max_height |
array
|
auto
xs
sm
md
lg
xl
xxl
xxxl
|
overflow_x |
array
|
visible
hidden
scroll
auto
|
overflow_y |
array
|
visible
hidden
scroll
auto
|
margin |
array
|
none
xxs
xs
sm
md
lg
xl
|
margin_bottom |
array
|
none
xxs
xs
sm
md
lg
xl
|
margin_left |
array
|
none
xxs
xs
sm
md
lg
xl
|
margin_right |
array
|
none
xxs
xs
sm
md
lg
xl
|
margin_top |
array
|
none
xxs
xs
sm
md
lg
xl
|
margin_x |
array
|
none
xxs
xs
sm
md
lg
xl
|
margin_y |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding_bottom |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding_left |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding_right |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding_top |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding_x |
array
|
none
xxs
xs
sm
md
lg
xl
|
padding_y |
array
|
none
xxs
xs
sm
md
lg
xl
|
classname |
string
|
|
dark |
boolean
|
|
group_hover |
boolean
|
|
id |
string
|
|
data |
hashprop
|
|
aria |
hashprop
|
|
html_options |
hashprop
|
|
children |
proc
|
|
style |
hashprop
|
Props | Type | Values | Default |
---|---|---|---|
form_system |
base
|
||
form_system_options |
base
|
||
loading |
boolean
|
false
|
|
options |
base
|
||
validate |
boolean
|
false
|