Wagtailstreamforms is a great package to build forms very easily, it allows for fields to be stacked, but how do I choose fields to render side-by-side? Below is a picture of my desired outcome.
Notice how the name and email fields and project timespan and budget fields are in the same row and are side-by-side. I want to add functionality where I can do this with any two types of fields, where I simply select the fields in the wagtailstreamforms form builder settings page.
How do I go about doing this?
Edit: Some extra details.
Right now wagtailstreamforms can only build forms with fields stacked one on top of the other. There is no functionality in the form builder page to create a grid system with form fields in multiple columns.
I want to add functionality where I can create a new field via the register decorator in wagtailstreamforms_fields.py, where the field will have two options, left and right, and each option will be a list of the available fields (excluding this one) so the user can create two fields to place side-by-side in two columns.
I have started to implement this by creating the field with the left and right options.
# wagtailstreamforms_fields.py
from django import forms
from wagtail.core import blocks
from wagtail.core.fields import StreamField
from wagtailstreamforms import streamfield
from wagtailstreamforms.fields import BaseField, register
form_field_list_left = streamfield.FormFieldStreamBlock('form_two_columns_left')
form_field_list_right = streamfield.FormFieldStreamBlock('form_two_columns_left')
@register('two_columns')
class TwoColumns(BaseField):
field_class = forms.MultiValueField
widget = forms.MultiWidget
icon = 'list-ul'
label = 'Two Columns'
def get_form_block(self):
return blocks.StructBlock([
('left', form_field_list_left),
('right', form_field_list_right),
], icon=self.icon, label=self.label)
The issue now is how do I access the left and right options in the templates?
If the two-column field works, I will be expanding this by creating a few other fields for three-columns and maybe even four-columns.
Another edit: a different way to achieve this grid system.
As I'm using Bootstrap CSS for my project, I can specify an extra option called width for each form, this will be a number from 1 to 12, 12 being full width.
I can then access this option in templates and append it to the CSS class of the container div, col-{{ field.width }}
This method allows more flexibility to the grid system's size, although it isn't as intuitive as having fields with preset column widths. I will be pursuing this approach in another StackOverflow post: Adding additional information into each form field


