File: bootstrap2-rendering.php

package info (click to toggle)
php-nette 2.4-20160731-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,208 kB
  • ctags: 7,552
  • sloc: php: 31,410; makefile: 6
file content (96 lines) | stat: -rw-r--r-- 2,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

/**
 * Nette Forms & Bootstap 2 rendering example.
 */


if (@!include 'Nette/loader.php') {
	die('Install packages using `composer install`');
}

use Nette\Forms\Form;
use Tracy\Debugger;
use Tracy\Dumper;

Debugger::enable();


$form = new Form;

$form->addGroup('Personal data');
$form->addText('name', 'Your name')
	->setRequired('Enter your name');

$form->addRadioList('gender', 'Your gender', [
	'male', 'female',
]);

$form->addCheckboxList('colors', 'Favorite colors:', [
	'red', 'green', 'blue',
]);

$form->addSelect('country', 'Country', [
	'Buranda', 'Qumran', 'Saint Georges Island',
]);

$form->addCheckbox('send', 'Ship to address');

$form->addGroup('Your account');
$form->addPassword('password', 'Choose password');
$form->addUpload('avatar', 'Picture');
$form->addTextArea('note', 'Comment');

$form->addGroup();
$form->addSubmit('submit', 'Send');
$form->addSubmit('cancel', 'Cancel');


if ($form->isSuccess()) {
	echo '<h2>Form was submitted and successfully validated</h2>';
	Dumper::dump($form->getValues());
	exit;
}



// setup form rendering
$renderer = $form->getRenderer();
$renderer->wrappers['controls']['container'] = NULL;
$renderer->wrappers['pair']['container'] = 'div class=control-group';
$renderer->wrappers['pair']['.error'] = 'error';
$renderer->wrappers['control']['container'] = 'div class=controls';
$renderer->wrappers['label']['container'] = 'div class=control-label';
$renderer->wrappers['control']['description'] = 'span class=help-inline';
$renderer->wrappers['control']['errorcontainer'] = 'span class=help-inline';

// make form and controls compatible with Twitter Bootstrap
$form->getElementPrototype()->class('form-horizontal');

foreach ($form->getControls() as $control) {
	$type = $control->getOption('type');
	if ($type === 'button') {
		$control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn');
		$usedPrimary = TRUE;

	} elseif (in_array($type, ['checkbox', 'radio'], TRUE)) {
		$control->getLabelPrototype()->addClass($type);
		$control->getSeparatorPrototype()->setName(NULL);
	}
}


?>
<!DOCTYPE html>
<meta charset="utf-8">
<title>Nette Forms & Bootstrap 2 rendering example</title>

<link rel="stylesheet" media="screen" href="https://netdna.bootstrapcdn.com/bootstrap/2.3.2/css/bootstrap.min.css" />

<div class="container">
	<div class="page-header">
		<h1>Nette Forms & Bootstrap 2 rendering example</h1>
	</div>

	<?php echo $form ?>
</div>