Skip to content

Button styles ​

The renderers give the first button without a btn class btn btn-primary and every following one btn btn-default. A button whose class already contains btn is left alone and does not use up the primary style. This form has one button of each kind:

php
<?php

declare(strict_types=1);

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

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

// The renderers give the first button without a "btn" class btn-primary and every following one
// btn-default. A button whose class already contains "btn" is left as it is and does not use up
// the primary style.
$form = new Form('article');
$form->setAction('/article/42');
$form->addText('title', 'Title')->setDefaultValue('Hello, Bootstrap');

// first button without a btn class: btn btn-primary
$form->addSubmit('save', 'Save');

// own Bootstrap classes, set as a string: left alone
$form->addSubmit('delete', 'Delete')
    ->setHtmlAttribute('class', 'btn btn-danger');

// own classes without "btn": btn btn-default is added next to them
$form->addSubmit('draft', 'Save as draft')
    ->setHtmlAttribute('class', 'js-draft');

// a class added with addClass() is checked the same way: btn-link contains "btn", left alone
$form->addSubmit('cancel', 'Cancel')
    ->setValidationScope([])
    ->getControlPrototype()->addClass('btn btn-link');

// a plain <input type="button">: btn btn-default
$form->addButton('preview', 'Preview');

$form->setRenderer(new BootstrapVerticalRenderer());

echo $form;

Preview ​

examples/button-styles · Bootstrap 3.4.1Open ↗
ButtonClass set in PHPRendered class
Savenonebtn btn-primary button
Deletebtn btn-dangerbtn btn-danger button
Save as draftjs-draftjs-draft btn btn-default button
Cancelbtn btn-link with addClass()btn btn-link button
Previewnone (addButton())btn btn-default button

The trailing button class comes from the control .submit and .button entries of $wrappers (see Configuration).

Output ​

html
<form action="/article/42" method="post" id="frm-article" novalidate="novalidate">


<div class="form-group">
	<label for="frm-article-title">Title</label>

	<input type="text" name="title" class="form-control text" id="frm-article-title" value="Hello, Bootstrap">
</div>

<div class="form-group">
	

	<input type="submit" name="save" class="btn btn-primary button" value="Save"> <input type="submit" name="delete" class="btn btn-danger button" value="Delete"> <input type="submit" name="draft" class="js-draft btn btn-default button" value="Save as draft"> <input type="submit" name="cancel" class="btn btn-link button" value="Cancel" formnovalidate> <input type="button" name="preview" class="btn btn-default button" value="Preview">
</div>


<div><input type="hidden" name="_form_" value="article"></div>
</form>

The buttons follow each other, so they share one form-group. Cancel skips validation (setValidationScope([])), which nette/forms renders as formnovalidate.

Run it ​

bash
git clone https://github.com/tomaj/nette-bootstrap-form.git
cd nette-bootstrap-form/examples/button-styles
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.