FormGroup
The FormGroup component wraps input + label + help text + validation feedback in one reusable component.
Quick Start
FormGroup(
Input(name="email", type="email"),
label="Email Address",
help_text="We'll never share your email",
)
Usage Scenarios
With Validation Error
FormGroup(
Input(name="password", type="password"),
label="Password",
error="Password must be at least 8 characters",
is_invalid=True,
)
With Success State
FormGroup(
Input(name="username", value="john_doe"),
label="Username",
success="Username is available!",
is_valid=True,
)
Required Field
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
input_element |
Any |
Required | Input, Select, or Textarea component |
label |
str \| None |
None |
Label text (optional) |
help_text |
str \| None |
None |
Help text shown below input |
error |
str \| None |
None |
Error message (shown when is_invalid=True) |
success |
str \| None |
None |
Success message (shown when is_valid=True) |
is_invalid |
bool |
False |
Whether to show invalid state |
is_valid |
bool |
False |
Whether to show valid state |
required |
bool |
False |
Whether field is required (adds asterisk to label) |
**kwargs |
Any |
- | Additional HTML attributes for the container |
Accessibility
- Labels are associated with inputs via
forandidattributes. - Required fields display a red asterisk.
- Validation feedback uses
invalid-feedback d-blockandvalid-feedback d-block. - Help text uses
form-text text-muted.
API Reference
faststrap.components.forms.formgroup.FormGroup(input_element, label=None, help_text=None, error=None, success=None, is_invalid=False, is_valid=False, required=False, **kwargs)
Form group with label, input, help text, and validation feedback.
Wraps Bootstrap form controls with proper structure and validation states. Eliminates boilerplate for form field creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_element
|
Any
|
Input, Select, or Textarea component |
required |
label
|
str | None
|
Label text (optional) |
None
|
help_text
|
str | None
|
Help text shown below input |
None
|
error
|
str | None
|
Error message (shown when is_invalid=True) |
None
|
success
|
str | None
|
Success message (shown when is_valid=True) |
None
|
is_invalid
|
bool
|
Whether to show invalid state |
False
|
is_valid
|
bool
|
Whether to show valid state |
False
|
required
|
bool
|
Whether field is required (adds asterisk to label) |
False
|
**kwargs
|
Any
|
Additional HTML attributes for the container |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
Div containing label, input, and feedback |
Examples:
Basic form group:
>>> FormGroup(
... Input(name="email", type="email"),
... label="Email Address",
... help_text="We'll never share your email"
... )
With validation error:
>>> FormGroup(
... Input(name="password", type="password"),
... label="Password",
... error="Password must be at least 8 characters",
... is_invalid=True
... )
With success state:
>>> FormGroup(
... Input(name="username", value="john_doe"),
... label="Username",
... success="Username is available!",
... is_valid=True
... )
Required field:
Note
The input_element should be a FastHTML Input, Select, or Textarea. Validation classes (is-invalid, is-valid) are automatically added to the input element based on is_invalid/is_valid flags.
Source code in src/faststrap/components/forms/formgroup.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |