Skip to content

Commit a0bc775

Browse files
committed
added examples
1 parent b48d57d commit a0bc775

14 files changed

+1024
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"require-dev": {
2323
"nette/tester": "~1.0",
24-
"latte/latte": "~2.2"
24+
"latte/latte": "~2.2",
25+
"tracy/tracy": "~2.2"
2526
},
2627
"autoload": {
2728
"classmap": ["src/"]

examples/assets/logo.png

1.31 KB
Loading

examples/assets/style.css

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/** common style for Nette examples */
2+
3+
html {
4+
font: 16px/1.5 sans-serif;
5+
border-top: 4.7em solid #F4EBDB;
6+
}
7+
8+
body {
9+
max-width: 990px;
10+
margin: -4.7em auto 0;
11+
background: white;
12+
color: #333;
13+
}
14+
15+
h1 {
16+
font-size: 1.9em;
17+
margin: .5em 0 1.5em;
18+
background: url(logo.png) right center no-repeat;
19+
color: #7A7772;
20+
text-shadow: 1px 1px 0 white;
21+
}
22+
23+
fieldset {
24+
padding: .2em 1em 1em;
25+
margin: .5em 0;
26+
background: #E4F1FC;
27+
border: 1px solid #B2D1EB;
28+
}
29+
30+
textarea, select, input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) {
31+
padding: .3em .5em;
32+
color: black;
33+
background: white;
34+
border: 1px solid silver;
35+
}
36+
37+
select {
38+
padding-right: .3em;
39+
}
40+
41+
input[type="submit"] {
42+
font-size: 120%;
43+
}
44+
45+
th {
46+
width: 10em;
47+
text-align: right;
48+
font-weight: normal;
49+
}
50+
51+
.required label {
52+
font-weight: bold;
53+
}
54+
55+
.error {
56+
color: #E22;
57+
font-weight: bold;
58+
}
59+
60+
footer a {
61+
font-size: 70%;
62+
color: gray;
63+
}

examples/basic-example.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
/**
4+
* Nette Forms basic example.
5+
*/
6+
7+
8+
if (@!include __DIR__ . '/../vendor/autoload.php') {
9+
die('Install packages using `composer update --dev`');
10+
}
11+
12+
use Nette\Forms\Form,
13+
Tracy\Debugger,
14+
Tracy\Dumper,
15+
Nette\Utils\Html;
16+
17+
Debugger::enable();
18+
19+
20+
$form = new Form;
21+
22+
// group Personal data
23+
$form->addGroup('Personal data')
24+
->setOption('description', 'We value your privacy and we ensure that the information you give to us will not be shared to other entities.');
25+
26+
$form->addText('name', 'Your name:')
27+
->setRequired('Enter your name');
28+
29+
$form->addText('age', 'Your age:')
30+
->setRequired('Enter your age')
31+
->addRule($form::INTEGER, 'Age must be numeric value')
32+
->addRule($form::RANGE, 'Age must be in range from %d to %d', array(10, 100));
33+
34+
$form->addRadioList('gender', 'Your gender:', array(
35+
'm' => 'male',
36+
'f' => 'female',
37+
));
38+
39+
$form->addCheckboxList('colors', 'Favorite colors:', array(
40+
'r' => 'red',
41+
'g' => 'green',
42+
'b' => 'blue',
43+
));
44+
45+
$form->addText('email', 'Email:')
46+
->setEmptyValue('@')
47+
->addCondition($form::FILLED) // conditional rule: if is email filled, ...
48+
->addRule($form::EMAIL, 'Incorrect email address'); // ... then check email
49+
50+
51+
// group Shipping address
52+
$form->addGroup('Shipping address')
53+
->setOption('embedNext', TRUE);
54+
55+
$form->addCheckbox('send', 'Ship to address')
56+
->addCondition($form::FILLED) // conditional rule: if is checkbox checked...
57+
->toggle('sendBox'); // toggle div #sendBox
58+
59+
60+
// subgroup
61+
$form->addGroup()
62+
->setOption('container', Html::el('div')->id('sendBox'));
63+
64+
$form->addText('street', 'Street:');
65+
66+
$form->addText('city', 'City:')
67+
->addConditionOn($form['send'], $form::FILLED)
68+
->setRequired('Enter your shipping address');
69+
70+
$countries = array(
71+
'World' => array(
72+
'bu' => 'Buranda',
73+
'qu' => 'Qumran',
74+
'st' => 'Saint Georges Island',
75+
),
76+
'?' => 'other',
77+
);
78+
$form->addSelect('country', 'Country:', $countries)
79+
->setPrompt('Select your country')
80+
->addConditionOn($form['send'], $form::FILLED)
81+
->setRequired('Select your country');
82+
83+
84+
// group Your account
85+
$form->addGroup('Your account');
86+
87+
$form->addPassword('password', 'Choose password:')
88+
->setRequired('Choose your password')
89+
->addRule($form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3);
90+
91+
$form->addPassword('password2', 'Reenter password:')
92+
->setRequired('Reenter your password')
93+
->addRule($form::EQUAL, 'Passwords do not match', $form['password']);
94+
95+
$form->addUpload('avatar', 'Picture:')
96+
->addCondition($form::FILLED)
97+
->addRule($form::IMAGE, 'Uploaded file is not image');
98+
99+
$form->addHidden('userid');
100+
101+
$form->addTextArea('note', 'Comment:');
102+
103+
// group for buttons
104+
$form->addGroup();
105+
106+
$form->addSubmit('submit', 'Send');
107+
108+
109+
$form->setDefaults(array(
110+
'name' => 'John Doe',
111+
'userid' => 231,
112+
));
113+
114+
115+
if ($form->isSuccess()) {
116+
echo '<h2>Form was submitted and successfully validated</h2>';
117+
Dumper::dump($form->getValues());
118+
exit;
119+
}
120+
121+
122+
?>
123+
<!DOCTYPE html>
124+
<meta charset="utf-8">
125+
<title>Nette Forms basic example</title>
126+
<link rel="stylesheet" media="screen" href="assets/style.css" />
127+
<script src="http://nette.github.com/resources/js/netteForms.js"></script>
128+
129+
<h1>Nette Forms basic example</h1>
130+
131+
<?php echo $form ?>
132+
133+
<footer><a href="http://doc.nette.org/en/forms">see documentation</a></footer>

examples/bootstrap2-rendering.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/**
4+
* Nette Forms & Bootstap 2 rendering example.
5+
*/
6+
7+
8+
if (@!include __DIR__ . '/../vendor/autoload.php') {
9+
die('Install packages using `composer update --dev`');
10+
}
11+
12+
use Nette\Forms\Form,
13+
Nette\Forms\Controls,
14+
Tracy\Debugger,
15+
Tracy\Dumper;
16+
17+
Debugger::enable();
18+
19+
20+
$form = new Form;
21+
22+
$form->addGroup('Personal data');
23+
$form->addText('name', 'Your name')
24+
->setRequired('Enter your name');
25+
26+
$form->addRadioList('gender', 'Your gender', array(
27+
'male', 'female',
28+
));
29+
30+
$form->addCheckboxList('colors', 'Favorite colors:', array(
31+
'red', 'green', 'blue',
32+
));
33+
34+
$form->addSelect('country', 'Country', array(
35+
'Buranda', 'Qumran', 'Saint Georges Island',
36+
));
37+
38+
$form->addCheckbox('send', 'Ship to address');
39+
40+
$form->addGroup('Your account');
41+
$form->addPassword('password', 'Choose password');
42+
$form->addUpload('avatar', 'Picture');
43+
$form->addTextArea('note', 'Comment');
44+
45+
$form->addGroup();
46+
$form->addSubmit('submit', 'Send');
47+
$form->addSubmit('cancel', 'Cancel');
48+
49+
50+
if ($form->isSuccess()) {
51+
echo '<h2>Form was submitted and successfully validated</h2>';
52+
Dumper::dump($form->getValues());
53+
exit;
54+
}
55+
56+
57+
58+
// setup form rendering
59+
$renderer = $form->getRenderer();
60+
$renderer->wrappers['controls']['container'] = NULL;
61+
$renderer->wrappers['pair']['container'] = 'div class=control-group';
62+
$renderer->wrappers['pair']['.error'] = 'error';
63+
$renderer->wrappers['control']['container'] = 'div class=controls';
64+
$renderer->wrappers['label']['container'] = 'div class=control-label';
65+
$renderer->wrappers['control']['description'] = 'span class=help-inline';
66+
$renderer->wrappers['control']['errorcontainer'] = 'span class=help-inline';
67+
68+
// make form and controls compatible with Twitter Bootstrap
69+
$form->getElementPrototype()->class('form-horizontal');
70+
71+
foreach ($form->getControls() as $control) {
72+
if ($control instanceof Controls\Button) {
73+
$control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn');
74+
$usedPrimary = TRUE;
75+
76+
} elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
77+
$control->getLabelPrototype()->addClass($control->getControlPrototype()->type);
78+
$control->getSeparatorPrototype()->setName(NULL);
79+
}
80+
}
81+
82+
83+
?>
84+
<!DOCTYPE html>
85+
<meta charset="utf-8">
86+
<title>Nette Forms & Bootstrap 2 rendering example</title>
87+
88+
<link rel="stylesheet" media="screen" href="http://netdna.bootstrapcdn.com/bootstrap/2.3.2/css/bootstrap.min.css" />
89+
90+
<div class="container">
91+
<div class="page-header">
92+
<h1>Nette Forms & Bootstrap 2 rendering example</h1>
93+
</div>
94+
95+
<?php echo $form ?>
96+
</div>

0 commit comments

Comments
 (0)