PluginProbe
Packeta / 1.6.2
Packeta v1.6.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / forms / examples / bootstrap4-rendering.php

bootstrap4-rendering.php in Packeta 1.6.2, at deps/nette/forms/examples/bootstrap4-rendering.php

82 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Nette Forms & Bootstap v4 rendering example.
5 */
6 declare (strict_types=1);
7 namespace Packetery;
8
9 if (@(!(include __DIR__ . '/../vendor/autoload.php'))) {
10 die('Install packages using `composer install`');
11 }
12 use Packetery\Nette\Forms\Form;
13 use Packetery\Tracy\Debugger;
14 use Packetery\Tracy\Dumper;
15 Debugger::enable();
16 function makeBootstrap4(Form $form) : void
17 {
18 $renderer = $form->getRenderer();
19 $renderer->wrappers['controls']['container'] = null;
20 $renderer->wrappers['pair']['container'] = 'div class="form-group row"';
21 $renderer->wrappers['pair']['.error'] = 'has-danger';
22 $renderer->wrappers['control']['container'] = 'div class=col-sm-9';
23 $renderer->wrappers['label']['container'] = 'div class="col-sm-3 col-form-label"';
24 $renderer->wrappers['control']['description'] = 'span class=form-text';
25 $renderer->wrappers['control']['errorcontainer'] = 'span class=form-control-feedback';
26 $renderer->wrappers['control']['.error'] = 'is-invalid';
27 foreach ($form->getControls() as $control) {
28 $type = $control->getOption('type');
29 if ($type === 'button') {
30 $control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-secondary');
31 $usedPrimary = \true;
32 } elseif (\in_array($type, ['text', 'textarea', 'select'], \true)) {
33 $control->getControlPrototype()->addClass('form-control');
34 } elseif ($type === 'file') {
35 $control->getControlPrototype()->addClass('form-control-file');
36 } elseif (\in_array($type, ['checkbox', 'radio'], \true)) {
37 if ($control instanceof \Packetery\Nette\Forms\Controls\Checkbox) {
38 $control->getLabelPrototype()->addClass('form-check-label');
39 } else {
40 $control->getItemLabelPrototype()->addClass('form-check-label');
41 }
42 $control->getControlPrototype()->addClass('form-check-input');
43 $control->getSeparatorPrototype()->setName('div')->addClass('form-check');
44 }
45 }
46 }
47 $form = new Form();
48 $form->onRender[] = 'makeBootstrap4';
49 $form->addGroup('Personal data');
50 $form->addText('name', 'Your name')->setRequired('Enter your name');
51 $form->addRadioList('gender', 'Your gender', ['male', 'female']);
52 $form->addCheckboxList('colors', 'Favorite colors', ['red', 'green', 'blue']);
53 $form->addSelect('country', 'Country', ['Buranda', 'Qumran', 'Saint Georges Island']);
54 $form->addCheckbox('send', 'Ship to address');
55 $form->addGroup('Your account');
56 $form->addPassword('password', 'Choose password');
57 $form->addUpload('avatar', 'Picture');
58 $form->addTextArea('note', 'Comment');
59 $form->addGroup();
60 $form->addSubmit('submit', 'Send');
61 $form->addSubmit('cancel', 'Cancel');
62 if ($form->isSuccess()) {
63 echo '<h2>Form was submitted and successfully validated</h2>';
64 Dumper::dump($form->getValues());
65 exit;
66 }
67 ?>
68 <!DOCTYPE html>
69 <meta charset="utf-8">
70 <title>Nette Forms & Bootstrap v4 rendering example</title>
71
72 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
73
74 <div class="container">
75 <h1>Nette Forms & Bootstrap v4 rendering example</h1>
76
77 <?php
78 $form->render();
79 ?>
80 </div>
81 <?php
82