Skip to content

Every control type ​

A profile form with one control of every type that nette/forms has, rendered by the horizontal renderer. It is the reference for what the renderer adds to which control; the table is in Controls.

php
<?php

declare(strict_types=1);

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

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

// Every control type that nette/forms has, rendered by the horizontal renderer.
$form = new Form('profile');
$form->setAction('/profile');

// text, number, date and time inputs get form-control; the colour picker does not, because
// Bootstrap 3 has no style for colour inputs
$form->addText('name', 'Name')->setHtmlAttribute('placeholder', 'Jane Doe');
$form->addPassword('password', 'Password');
$form->addEmail('email', 'E-mail');
$form->addInteger('age', 'Age');
$form->addFloat('height', 'Height (m)');
$form->addDate('birthday', 'Birthday');
$form->addTime('alarm', 'Wake-up time');
$form->addDateTime('meeting', 'Next meeting');
$form->addColor('color', 'Favourite colour')->setDefaultValue('#7952b3');
$form->addTextArea('bio', 'About you', rows: 3);

// selects: form-control as well
$form->addSelect('country', 'Country', ['sk' => 'Slovakia', 'cz' => 'Czechia', 'at' => 'Austria'])
    ->setPrompt('Choose a country');
$form->addMultiSelect('languages', 'Languages', ['en' => 'English', 'sk' => 'Slovak', 'de' => 'German']);

// checkbox and lists: wrapped in div.checkbox / div.radio
$form->addCheckbox('terms', 'I agree with the terms');
$form->addCheckboxList('topics', 'Topics', ['php' => 'PHP', 'nette' => 'Nette', 'css' => 'CSS']);
$form->addRadioList('plan', 'Plan', ['free' => 'Free', 'pro' => 'Pro'])->setDefaultValue('free');

// files: no extra class, Bootstrap 3 styles file inputs itself. nette/forms adds a size limit
// (and a count limit for several files) from php.ini; set your own instead, so the rules do not
// depend on the server's configuration
$form->addUpload('avatar', 'Avatar')
    ->getRules()->removeRule(Form::MaxFileSize)
    ->addRule(Form::MaxFileSize, 'The avatar can be up to 2 MB.', 2 * 1024 * 1024);
$form->addMultiUpload('attachments', 'Attachments')
    ->getRules()->removeRule(Form::MaxFileSize)->removeRule(Form::MaxLength)
    ->addRule(Form::MaxFileSize, 'Each attachment can be up to 10 MB.', 10 * 1024 * 1024)
    ->addRule(Form::MaxLength, 'Attach at most %d files.', 5);

// hidden fields are collected in one <div> at the end of the form
$form->addHidden('referrer', 'newsletter');

// buttons: the first one is primary, the others default
$form->addSubmit('save', 'Save');
$form->addButton('preview', 'Preview');
$form->addImageButton('publish', '/img/publish.png', 'Publish');

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

echo $form;

Preview ​

examples/all-controls · Bootstrap 3.4.1Open ↗

What to look for:

  • Text, password, e-mail, number, date, time and date-time inputs, the text area and both selects have form-control.
  • The colour input does not: Bootstrap 3 has no style for it, so it keeps the browser's look; see Date, time and colour inputs.
  • The checkbox is wrapped in div.checkbox, each item of the checkbox list in div.checkbox and each radio in div.radio.
  • The file inputs have only the text class from $wrappers. Their size and count limits are set explicitly, because the defaults come from php.ini and would differ between machines.
  • The three buttons share one form-group: the submit button is btn-primary, the button and the image button btn-default.
  • The hidden field is rendered in a <div> just before </form>, after the _form_ field.

Output ​

html
<form action="/profile" method="post" id="frm-profile" enctype="multipart/form-data" class="form-horizontal" novalidate="novalidate">

<div>
<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-name">Name</label></div>

	<div class="col-sm-9"><input type="text" name="name" placeholder="Jane Doe" class="form-control text" id="frm-profile-name"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-password">Password</label></div>

	<div class="col-sm-9"><input type="password" name="password" class="form-control text" id="frm-profile-password"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-email">E-mail</label></div>

	<div class="col-sm-9"><input type="email" name="email" maxlength="255" class="form-control" id="frm-profile-email" data-nette-rules='[{"op":":email","msg":"Please enter a valid email address."}]'></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-age">Age</label></div>

	<div class="col-sm-9"><input type="number" name="age" class="form-control" id="frm-profile-age" data-nette-rules='[{"op":":integer","msg":"Please enter a valid integer."}]'></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-height">Height (m)</label></div>

	<div class="col-sm-9"><input type="number" name="height" step="any" class="form-control" id="frm-profile-height" data-nette-rules='[{"op":":float","msg":"Please enter a valid number."}]'></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-birthday">Birthday</label></div>

	<div class="col-sm-9"><input type="date" name="birthday" class="form-control" id="frm-profile-birthday"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-alarm">Wake-up time</label></div>

	<div class="col-sm-9"><input type="time" name="alarm" class="form-control" id="frm-profile-alarm"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-meeting">Next meeting</label></div>

	<div class="col-sm-9"><input type="datetime-local" name="meeting" class="form-control" id="frm-profile-meeting"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-color">Favourite colour</label></div>

	<div class="col-sm-9"><input type="color" name="color" id="frm-profile-color" value="#7952b3"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-bio">About you</label></div>

	<div class="col-sm-9"><textarea name="bio" rows="3" class="form-control" id="frm-profile-bio"></textarea></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-country">Country</label></div>

	<div class="col-sm-9"><select name="country" class="form-control" id="frm-profile-country"><option value="">Choose a country</option><option value="sk">Slovakia</option><option value="cz">Czechia</option><option value="at">Austria</option></select></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-languages">Languages</label></div>

	<div class="col-sm-9"><select name="languages[]" class="form-control" id="frm-profile-languages" multiple><option value="en">English</option><option value="sk">Slovak</option><option value="de">German</option></select></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"></div>

	<div class="col-sm-9"><div class="checkbox"><label for="frm-profile-terms"><input type="checkbox" name="terms" id="frm-profile-terms">I agree with the terms</label></div></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label>Topics</label></div>

	<div class="col-sm-9"><div class="checkbox"><label><input type="checkbox" name="topics[]" value="php">PHP</label></div><div class="checkbox"><label><input type="checkbox" name="topics[]" value="nette">Nette</label></div><div class="checkbox"><label><input type="checkbox" name="topics[]" value="css">CSS</label></div></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label>Plan</label></div>

	<div class="col-sm-9"><div class="radio"><label><input type="radio" name="plan" checked value="free">Free</label></div><div class="radio"><label><input type="radio" name="plan" value="pro">Pro</label></div></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-avatar">Avatar</label></div>

	<div class="col-sm-9"><input type="file" name="avatar" id="frm-profile-avatar" data-nette-rules='[{"op":":fileSize","msg":"The avatar can be up to 2 MB.","arg":2097152}]' class="text"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-profile-attachments">Attachments</label></div>

	<div class="col-sm-9"><input type="file" name="attachments[]" multiple id="frm-profile-attachments" data-nette-rules='[{"op":":fileSize","msg":"Each attachment can be up to 10 MB.","arg":10485760},{"op":":maxLength","msg":"Attach at most 5 files.","arg":5}]' class="text"></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"></div>

	<div class="col-sm-9"><input type="submit" name="save" class="btn btn-primary button" value="Save"> <input type="button" name="preview" class="btn btn-default button" value="Preview"> <input type="image" name="publish[]" src="/img/publish.png" alt="Publish" class="btn btn-default imagebutton"></div>
</div>
</div>

<div><input type="hidden" name="_form_" value="profile"><input type="hidden" name="referrer" value="newsletter"></div>
</form>

Run it ​

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