Skip to content

Manual rendering ​

A sign-in form placed into a Bootstrap panel. The form is rendered part by part, so the panel's markup can go around it, and by a small renderer that wraps BootstrapVerticalRenderer to add an ajax class: the Bootstrap renderers are final, so this is how you change the form before they render it.

php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Nette\Forms\Form;
use Nette\Forms\FormRenderer;
use Tomaj\Form\Renderer\BootstrapVerticalRenderer;

// A renderer cannot be extended (the classes are final), so a decorator changes the form
// before the Bootstrap renderer does its work: here it adds an "ajax" class to the <form>.
final class AjaxFormRenderer implements FormRenderer
{
    public function __construct(
        private readonly BootstrapVerticalRenderer $inner = new BootstrapVerticalRenderer(),
    ) {}

    public function render(Form $form, ?string $mode = null): string
    {
        $form->getElementPrototype()->addClass('ajax');

        return $this->inner->render($form, $mode);
    }
}

$form = new Form('login');
$form->setAction('/sign/in');
$form->addEmail('email', 'E-mail')->setRequired('Please enter your e-mail.');
$form->addPassword('password', 'Password')->setRequired('Please enter your password.');
$form->addCheckbox('remember', 'Keep me signed in');
$form->addSubmit('login', 'Sign in');
$form->addError('The e-mail or the password is wrong.');

$renderer = new AjaxFormRenderer();

// The parts of the form placed into a Bootstrap panel. The first call decorates the controls,
// so every part has the Bootstrap classes, exactly as when the form is rendered at once.
echo '<div class="panel panel-default">', "\n";
echo '<div class="panel-heading"><h3 class="panel-title">Sign in</h3></div>', "\n";
echo '<div class="panel-body">', "\n";
echo $renderer->render($form, 'begin');
echo $renderer->render($form, 'errors');
echo $renderer->render($form, 'body');
echo $renderer->render($form, 'end');
echo '</div>', "\n";
echo '<div class="panel-footer"><a href="/sign/forgot">Forgot your password?</a></div>', "\n";
echo '</div>', "\n";

Preview ​

examples/manual-rendering · Bootstrap 3.4.1Open ↗
  • AjaxFormRenderer implements Nette\Forms\FormRenderer, so it can be set with $form->setRenderer() like any renderer; here it is called directly.
  • begin prints the <form> tag, errors the alert with all errors, body the pairs, end the hidden fields and </form>.
  • The first render() call decorates the controls, so the later parts already have the Bootstrap classes.

Output ​

html
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">Sign in</h3></div>
<div class="panel-body">
<form action="/sign/in" method="post" id="frm-login" class="ajax" novalidate="novalidate">

<div class="alert alert-danger">
	<p>The e-mail or the password is wrong.</p>
</div>



<div class="form-group required">
	<label for="frm-login-email" class="required">E-mail</label>

	<input type="email" name="email" maxlength="255" class="form-control" id="frm-login-email" required data-nette-rules='[{"op":":filled","msg":"Please enter your e-mail."},{"op":":email","msg":"Please enter a valid email address."}]'>
</div>

<div class="form-group required">
	<label for="frm-login-password" class="required">Password</label>

	<input type="password" name="password" class="form-control text" id="frm-login-password" required data-nette-rules='[{"op":":filled","msg":"Please enter your password."}]'>
</div>

<div class="form-group">
	

	<div class="checkbox"><label for="frm-login-remember"><input type="checkbox" name="remember" id="frm-login-remember">Keep me signed in</label></div>
</div>

<div class="form-group">
	

	<input type="submit" name="login" class="btn btn-primary button" value="Sign in">
</div>


<div><input type="hidden" name="_form_" value="login"></div>
</form>
</div>
<div class="panel-footer"><a href="/sign/forgot">Forgot your password?</a></div>
</div>

The same in a Latte template is in Nette application.

Run it ​

bash
git clone https://github.com/tomaj/nette-bootstrap-form.git
cd nette-bootstrap-form/examples/manual-rendering
composer install
php run.php

The output is compared with expected-output.txt in CI on every pull request; see how the examples are tested.

Released under the MIT License.