API reference
Namespace Tomaj\Form\Renderer. The package has three public classes; each is a final subclass of Nette\Forms\Rendering\DefaultFormRenderer and implements Nette\Forms\FormRenderer.
| Class | Layout | Class added to <form> |
|---|---|---|
BootstrapRenderer | horizontal: labels in col-sm-3, controls in col-sm-9 | form-horizontal |
BootstrapVerticalRenderer | vertical: label above the control | none |
BootstrapInlineRenderer | inline: everything on one line | form-inline |
DecoratesControls in src/ is an internal trait shared by the three classes; it is not part of the public API and may change in any release.
Constructor
use Tomaj\Form\Renderer\BootstrapRenderer;
$renderer = new BootstrapRenderer(novalidate: true);| Parameter | Type | Default | Meaning |
|---|---|---|---|
$novalidate | bool | true | Add novalidate to the <form> element, so the browser skips its HTML5 validation and Nette shows the errors. false keeps the browser validation. |
The same constructor exists on all three classes.
render()
public function render(Nette\Forms\Form $form, ?string $mode = null): stringAdds the Bootstrap classes to the form and its controls, then renders it with DefaultFormRenderer::render(). $mode is null for the whole form, or 'begin', 'errors', 'ownerrors', 'body' or 'end' for one part. Calling it several times on the same form (or part by part) gives the same HTML; the classes are not added twice.
What changes on which control is listed in Renderers:
<form>: the form class (see the table above),novalidate="novalidate"unless disabledTextBasecontrols (text, password, text area, …),SelectBox,MultiSelectBox:form-controlButtoncontrols (submit, button, image):btn btn-primaryfor the first one without abtnclass,btn btn-defaultfor the following ones; buttons with abtnclass are untouchedCheckbox: wrapped in<div class="checkbox">CheckboxList,RadioList: each item wrapped in<div class="checkbox">/<div class="radio">
Usually you do not call render() yourself: set the renderer with $form->setRenderer($renderer) and render the form with echo $form, $form->render() or {control form} in Latte.
$wrappers
public array $wrappersThe wrapper definitions of DefaultFormRenderer (see the Nette documentation). Change them on the instance:
use Tomaj\Form\Renderer\BootstrapRenderer;
$renderer = new BootstrapRenderer();
$renderer->wrappers['control']['container'] = 'div class=col-sm-8';All three renderers start with these values:
| Key | Value |
|---|---|
error.container | div class="alert alert-danger" |
error.item | p |
group.container / group.label / group.description | fieldset / legend / p |
pair.container | div class=form-group |
pair..required / pair..error | required / has-error |
control.description | span class=help-block |
control.errorcontainer | span class=help-block |
control.erroritem | '' (no element per error) |
control.requiredsuffix, label.requiredsuffix | '' |
label.suffix | null |
hidden.container | div |
They differ in these keys:
| Key | BootstrapRenderer | BootstrapVerticalRenderer | BootstrapInlineRenderer |
|---|---|---|---|
form.container | null | null | '' |
controls.container | div | null | '' |
control.container | div class=col-sm-9 | '' | null |
label.container | div class="col-sm-3 control-label" | '' | '' |
null and '' both mean "no wrapper element".