Skip to content

Checkout with groups ​

A checkout form split into groups with addGroup(). Each group becomes a <fieldset> with a <legend>; a group description is a <p> under the legend, and control descriptions are help-blocks.

php
<?php

declare(strict_types=1);

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

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

// A checkout form split into groups: each group is a <fieldset> with a <legend> and an optional
// description. Controls that are in no group would be rendered after all groups.
$form = new Form('checkout');
$form->setAction('/checkout');

$form->addGroup('Contact');
$form->addEmail('email', 'E-mail')
    ->setRequired('Please enter your e-mail.')
    ->setOption('description', 'We send the order confirmation here.');
$form->addText('phone', 'Phone')
    ->setHtmlType('tel')
    ->setOption('description', 'Only for the courier.');

$form->addGroup('Shipping address')
    ->setOption('description', 'We deliver to Slovakia and Czechia.');
$form->addText('street', 'Street')->setRequired('Please enter the street.');
$form->addText('city', 'City')->setRequired('Please enter the city.');
$form->addText('zip', 'ZIP code')->setRequired('Please enter the ZIP code.');
$form->addSelect('country', 'Country', ['sk' => 'Slovakia', 'cz' => 'Czechia'])
    ->setDefaultValue('sk');

$form->addGroup('Delivery and payment');
$form->addRadioList('delivery', 'Delivery', [
    'courier' => 'Courier (2 days)',
    'pickup' => 'Pickup point (3 days)',
])->setDefaultValue('courier');
$form->addRadioList('payment', 'Payment', [
    'card' => 'Card',
    'transfer' => 'Bank transfer',
    'cod' => 'Cash on delivery',
])->setDefaultValue('card');
$form->addTextArea('note', 'Note for the courier', rows: 2);

// a group without a label: the buttons get their own fieldset without a legend
$form->addGroup();
$form->addCheckbox('terms', 'I agree with the terms and conditions')
    ->setRequired('You have to agree with the terms.');
$form->addSubmit('order', 'Place order');
$form->addSubmit('back', 'Back to cart')
    ->setValidationScope([]);

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

echo $form;

Preview ​

examples/checkout-groups · Bootstrap 3.4.1Open ↗
  • Every group has its own div controls container (the horizontal renderer's controls container) inside the fieldset.
  • The last group has no label, so its fieldset has no legend; it keeps the terms and the buttons apart from the payment fields.
  • Back to cart has setValidationScope([]), so it leaves without validating the form, and gets btn-default because it is the second button.

Output ​

html
<form action="/checkout" method="post" id="frm-checkout" class="form-horizontal" novalidate="novalidate">

<fieldset>
<legend>Contact</legend>

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

	<div class="col-sm-9"><input type="email" name="email" maxlength="255" class="form-control" id="frm-checkout-email" required data-nette-rules='[{"op":":filled","msg":"Please enter your e-mail."},{"op":":email","msg":"Please enter a valid email address."}]'> <span class="help-block">We send the order confirmation here.</span></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-checkout-phone">Phone</label></div>

	<div class="col-sm-9"><input type="tel" name="phone" class="form-control" id="frm-checkout-phone"> <span class="help-block">Only for the courier.</span></div>
</div>
</div>
</fieldset>

<fieldset>
<legend>Shipping address</legend>
<p>We deliver to Slovakia and Czechia.</p>

<div>
<div class="form-group required">
	<div class="col-sm-3 control-label"><label for="frm-checkout-street" class="required">Street</label></div>

	<div class="col-sm-9"><input type="text" name="street" class="form-control text" id="frm-checkout-street" required data-nette-rules='[{"op":":filled","msg":"Please enter the street."}]'></div>
</div>

<div class="form-group required">
	<div class="col-sm-3 control-label"><label for="frm-checkout-city" class="required">City</label></div>

	<div class="col-sm-9"><input type="text" name="city" class="form-control text" id="frm-checkout-city" required data-nette-rules='[{"op":":filled","msg":"Please enter the city."}]'></div>
</div>

<div class="form-group required">
	<div class="col-sm-3 control-label"><label for="frm-checkout-zip" class="required">ZIP code</label></div>

	<div class="col-sm-9"><input type="text" name="zip" class="form-control text" id="frm-checkout-zip" required data-nette-rules='[{"op":":filled","msg":"Please enter the ZIP code."}]'></div>
</div>

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

	<div class="col-sm-9"><select name="country" class="form-control" id="frm-checkout-country"><option value="sk" selected>Slovakia</option><option value="cz">Czechia</option></select></div>
</div>
</div>
</fieldset>

<fieldset>
<legend>Delivery and payment</legend>

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

	<div class="col-sm-9"><div class="radio"><label><input type="radio" name="delivery" checked value="courier">Courier (2 days)</label></div><div class="radio"><label><input type="radio" name="delivery" value="pickup">Pickup point (3 days)</label></div></div>
</div>

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

	<div class="col-sm-9"><div class="radio"><label><input type="radio" name="payment" checked value="card">Card</label></div><div class="radio"><label><input type="radio" name="payment" value="transfer">Bank transfer</label></div><div class="radio"><label><input type="radio" name="payment" value="cod">Cash on delivery</label></div></div>
</div>

<div class="form-group">
	<div class="col-sm-3 control-label"><label for="frm-checkout-note">Note for the courier</label></div>

	<div class="col-sm-9"><textarea name="note" rows="2" class="form-control" id="frm-checkout-note"></textarea></div>
</div>
</div>
</fieldset>

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

	<div class="col-sm-9"><div class="checkbox"><label for="frm-checkout-terms"><input type="checkbox" name="terms" id="frm-checkout-terms" required data-nette-rules='[{"op":":filled","msg":"You have to agree with the terms."}]'>I agree with the terms and conditions</label></div></div>
</div>

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

	<div class="col-sm-9"><input type="submit" name="order" class="btn btn-primary button" value="Place order"> <input type="submit" name="back" class="btn btn-default button" value="Back to cart" formnovalidate></div>
</div>
</div>
</fieldset>

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

Groups are rendered in the order they were added. Controls added before the first addGroup() are not in any group and are rendered after all groups.

Run it ​

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