Skip to content

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.

ClassLayoutClass added to <form>
BootstrapRendererhorizontal: labels in col-sm-3, controls in col-sm-9form-horizontal
BootstrapVerticalRenderervertical: label above the controlnone
BootstrapInlineRendererinline: everything on one lineform-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 ​

php
use Tomaj\Form\Renderer\BootstrapRenderer;

$renderer = new BootstrapRenderer(novalidate: true);
ParameterTypeDefaultMeaning
$novalidatebooltrueAdd 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() ​

php
public function render(Nette\Forms\Form $form, ?string $mode = null): string

Adds 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 disabled
  • TextBase controls (text, password, text area, …), SelectBox, MultiSelectBox: form-control
  • Button controls (submit, button, image): btn btn-primary for the first one without a btn class, btn btn-default for the following ones; buttons with a btn class are untouched
  • Checkbox: 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 ​

php
public array $wrappers

The wrapper definitions of DefaultFormRenderer (see the Nette documentation). Change them on the instance:

php
use Tomaj\Form\Renderer\BootstrapRenderer;

$renderer = new BootstrapRenderer();
$renderer->wrappers['control']['container'] = 'div class=col-sm-8';

All three renderers start with these values:

KeyValue
error.containerdiv class="alert alert-danger"
error.itemp
group.container / group.label / group.descriptionfieldset / legend / p
pair.containerdiv class=form-group
pair..required / pair..errorrequired / has-error
control.descriptionspan class=help-block
control.errorcontainerspan class=help-block
control.erroritem'' (no element per error)
control.requiredsuffix, label.requiredsuffix''
label.suffixnull
hidden.containerdiv

They differ in these keys:

KeyBootstrapRendererBootstrapVerticalRendererBootstrapInlineRenderer
form.containernullnull''
controls.containerdivnull''
control.containerdiv class=col-sm-9''null
label.containerdiv class="col-sm-3 control-label"''''

null and '' both mean "no wrapper element".

Released under the LGPL-2.0-or-later license.