|
| 1 | +{* Generic form template for Bootstrap v5 *} |
| 2 | + |
| 3 | +{define bootstrap-form, $name} |
| 4 | +<form n:name=$name> |
| 5 | + {* List for form-level error messages *} |
| 6 | + <ul class="alert alert-danger" n:ifcontent> |
| 7 | + <li n:foreach="$form->ownErrors as $error">{$error}</li> |
| 8 | + </ul> |
| 9 | + |
| 10 | + {include controls $form->getControls()} |
| 11 | +</form> |
| 12 | +{/define} |
| 13 | + |
| 14 | + |
| 15 | +{define local controls, array $controls} |
| 16 | + {* Loop over form controls and render each one *} |
| 17 | + <div n:foreach="$controls as $control" |
| 18 | + n:if="!$control->getOption(rendered) && $control->getOption(type) !== hidden" |
| 19 | + n:class="mb-3, row, $control->required ? required, $control->error ? is-invalid"> |
| 20 | + |
| 21 | + {* Label for the control *} |
| 22 | + <div class="col-sm-3 col-form-label">{label $control /}</div> |
| 23 | + |
| 24 | + <div class="col-sm-9"> |
| 25 | + {include control $control} |
| 26 | + {if $control->getOption(type) === button} |
| 27 | + {while $iterator->nextValue?->getOption(type) === button} |
| 28 | + {input $iterator->nextValue class => "btn btn-secondary"} |
| 29 | + {do $iterator->next()} |
| 30 | + {/while} |
| 31 | + {/if} |
| 32 | + |
| 33 | + {* Display control-level errors or descriptions, if present *} |
| 34 | + <span class=invalid-feedback n:ifcontent>{$control->error}</span> |
| 35 | + <span class=form-text n:ifcontent>{$control->getOption(description)}</span> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | +{/define} |
| 39 | + |
| 40 | + |
| 41 | +{define local control, Nette\Forms\Controls\BaseControl $control} |
| 42 | + {* Conditionally render controls based on their type with appropriate Bootstrap classes *} |
| 43 | + {if $control->getOption(type) in [text, select, textarea, datetime, file]} |
| 44 | + {input $control class => form-control} |
| 45 | + |
| 46 | + {elseif $control->getOption(type) === button} |
| 47 | + {input $control class => "btn btn-primary"} |
| 48 | + |
| 49 | + {elseif $control->getOption(type) in [checkbox, radio]} |
| 50 | + {var $items = $control instanceof Nette\Forms\Controls\Checkbox ? [''] : $control->getItems()} |
| 51 | + <div class="form-check" n:foreach="$items as $key => $foo"> |
| 52 | + {input $control:$key class => form-check-input}{label $control:$key class => form-check-label /} |
| 53 | + </div> |
| 54 | + |
| 55 | + {elseif $control->getOption(type) === color} |
| 56 | + {input $control class => "form-control form-control-color"} |
| 57 | + |
| 58 | + {else} |
| 59 | + {input $control} |
| 60 | + {/if} |
| 61 | +{/define} |
0 commit comments