Skip to content

Inline filter ​

A filter above a list of orders. It is sent with GET, so the filter is in the URL and the filtered list can be bookmarked, and it fits on one line with BootstrapInlineRenderer.

php
<?php

declare(strict_types=1);

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

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

// A filter above an order list: one line, sent with GET so the filter ends up in the URL.
$form = new Form('filter');
$form->setMethod(Form::Get);
$form->setAction('/orders');

$form->addText('q', 'Search')
    ->setHtmlAttribute('placeholder', 'Order number or customer');
$form->addSelect('status', 'Status', [
    'new' => 'New',
    'paid' => 'Paid',
    'shipped' => 'Shipped',
    'cancelled' => 'Cancelled',
])->setPrompt('Any status');
$form->addDate('from', 'From');
$form->addSubmit('filter', 'Filter');
$form->addSubmit('clear', 'Clear')
    ->setValidationScope([]);

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

echo $form;

Preview ​

examples/inline-filter · Bootstrap 3.4.1Open ↗

From Bootstrap's sm breakpoint (768 px) the form-groups are inline-block and the inputs width: auto. On a phone they are stacked, as in the vertical layout.

Clear has setValidationScope([]), so it submits the form without validating it; nette/forms adds formnovalidate to it. Avoid naming a button reset or submit: those names collide with properties of the <form> element in the browser, and nette/forms renames them (to _reset).

The date input gets form-control like the other inputs (since 3.0, #33).

Output ​

html
<form action="/orders" method="get" id="frm-filter" class="form-inline" novalidate="novalidate">


<div class="form-group">
	<label for="frm-filter-q">Search</label>

	<input type="text" name="q" placeholder="Order number or customer" class="form-control text" id="frm-filter-q">
</div>

<div class="form-group">
	<label for="frm-filter-status">Status</label>

	<select name="status" class="form-control" id="frm-filter-status"><option value="">Any status</option><option value="new">New</option><option value="paid">Paid</option><option value="shipped">Shipped</option><option value="cancelled">Cancelled</option></select>
</div>

<div class="form-group">
	<label for="frm-filter-from">From</label>

	<input type="date" name="from" class="form-control" id="frm-filter-from">
</div>

<div class="form-group">
	

	<input type="submit" name="filter" class="btn btn-primary button" value="Filter"> <input type="submit" name="clear" class="btn btn-default button" value="Clear" formnovalidate>
</div>


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

Run it ​

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