Skip to content

Commit ed7ecd7

Browse files
committed
examples: updates
1 parent 8774b3f commit ed7ecd7

9 files changed

+129
-21
lines changed

examples/assets/style.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,21 @@ fieldset {
2727
border: 1px solid #B2D1EB;
2828
}
2929

30-
textarea, select, input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) {
30+
textarea,
31+
select,
32+
input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) {
3133
padding: .3em .5em;
3234
color: black;
3335
background: white;
3436
border: 1px solid silver;
3537
}
3638

39+
.has-error textarea,
40+
.has-error select,
41+
.has-error input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) {
42+
border-color: #E22;
43+
}
44+
3745
select {
3846
padding-right: .3em;
3947
}
@@ -55,6 +63,7 @@ th {
5563
.error {
5664
color: #E22;
5765
font-weight: bold;
66+
margin-left: 1em;
5867
}
5968

6069
footer a {

examples/basic-example.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@
4242
'b' => 'blue',
4343
]);
4444

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
45+
$form->addEmail('email', 'Email:')
46+
->setEmptyValue('@');
4947

5048

5149
// group Shipping address
@@ -93,8 +91,8 @@
9391
->addRule($form::EQUAL, 'Passwords do not match', $form['password']);
9492

9593
$form->addUpload('avatar', 'Picture:')
96-
->addCondition($form::FILLED)
97-
->addRule($form::IMAGE, 'Uploaded file is not image');
94+
->setRequired(FALSE)
95+
->addRule($form::IMAGE, 'Uploaded file is not image');
9896

9997
$form->addHidden('userid');
10098

@@ -114,7 +112,7 @@
114112

115113
if ($form->isSuccess()) {
116114
echo '<h2>Form was submitted and successfully validated</h2>';
117-
Dumper::dump($form->getValues());
115+
Dumper::dump($form->getValues(), [Dumper::COLLAPSE => FALSE]);
118116
exit;
119117
}
120118

examples/bootstrap2-rendering.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
$usedPrimary = TRUE;
7575

7676
} elseif (in_array($type, ['checkbox', 'radio'], TRUE)) {
77-
$control->getLabelPrototype()->addClass($control->getControlPrototype()->type);
77+
$control->getLabelPrototype()->addClass($type);
7878
$control->getSeparatorPrototype()->setName(NULL);
7979
}
8080
}

examples/bootstrap3-rendering.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
$control->getControlPrototype()->addClass('form-control');
7878

7979
} elseif (in_array($type, ['checkbox', 'radio'], TRUE)) {
80-
$control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
80+
$control->getSeparatorPrototype()->setName('div')->addClass($type);
8181
}
8282
}
8383

examples/custom-control.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public function setValue($value)
4646

4747

4848
/**
49-
* @return DateTime|NULL
49+
* @return DateTimeImmutable|NULL
5050
*/
5151
public function getValue()
5252
{
5353
return self::validateDate($this)
54-
? (new DateTime)->setDate($this->year, $this->month, $this->day)->setTime(0, 0)
54+
? (new DateTimeImmutable)->setDate($this->year, $this->month, $this->day)->setTime(0, 0)
5555
: NULL;
5656
}
5757

examples/custom-rendering.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
->setRequired('Enter your name');
3838

3939
$form->addRadioList('gender', 'Your gender', [
40-
'm' => Html::el('option', 'male')->style('color: #248bd3'),
41-
'f' => Html::el('option', 'female')->style('color: #e948d4'),
40+
'm' => Html::el('span', 'male')->style('color: #248bd3'),
41+
'f' => Html::el('span', 'female')->style('color: #e948d4'),
4242
]);
4343

4444
$form->addSelect('country', 'Country', [

examples/html5.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@
3636
->addRule($form::INTEGER, 'Precision must be numeric value')
3737
->addRule($form::RANGE, 'Precision must be in range from %d to %d', [0, 100]);
3838

39-
$form->addText('email', 'Send to email:')
40-
->setType('email')
39+
$form->addEmail('email', 'Send to email:')
4140
->setAttribute('autocomplete', 'off')
42-
->setAttribute('placeholder', 'Optional, but Recommended')
43-
->addCondition($form::FILLED) // conditional rule: if is email filled, ...
44-
->addRule($form::EMAIL, 'Incorrect email address'); // ... then check email
41+
->setAttribute('placeholder', 'Optional, but Recommended');
4542

4643
$form->addSubmit('submit', 'Send');
4744

examples/live-validation.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
* Nette Forms live validation example.
5+
*/
6+
7+
8+
if (@!include __DIR__ . '/../vendor/autoload.php') {
9+
die('Install packages using `composer install`');
10+
}
11+
12+
use Nette\Forms\Form;
13+
use Tracy\Debugger;
14+
use Tracy\Dumper;
15+
16+
Debugger::enable();
17+
18+
19+
$form = new Form;
20+
$form->addText('name', 'Your name:')
21+
->setRequired('Enter your name');
22+
23+
$form->addText('age', 'Your age:')
24+
->setRequired('Enter your age')
25+
->addRule($form::INTEGER, 'Age must be numeric value')
26+
->addRule($form::RANGE, 'Age must be in range from %d to %d', [10, 100]);
27+
28+
$form->addPassword('password', 'Choose password:')
29+
->setRequired('Choose your password')
30+
->addRule($form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3);
31+
32+
$form->addPassword('password2', 'Reenter password:')
33+
->setRequired('Reenter your password')
34+
->addRule($form::EQUAL, 'Passwords do not match', $form['password']);
35+
36+
$form->addSubmit('submit', 'Send');
37+
38+
39+
if ($form->isSuccess()) {
40+
echo '<h2>Form was submitted and successfully validated</h2>';
41+
Dumper::dump($form->getValues());
42+
exit;
43+
}
44+
45+
$renderer = $form->getRenderer();
46+
$renderer->wrappers['pair']['.error'] = 'has-error';
47+
48+
?>
49+
<!DOCTYPE html>
50+
<meta charset="utf-8">
51+
<title>Nette Forms live validation example</title>
52+
<link rel="stylesheet" media="screen" href="assets/style.css" />
53+
<script src="https://nette.github.io/resources/js/netteForms.js"></script>
54+
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
55+
56+
<script>
57+
function showErrors(errors, focus)
58+
{
59+
errors.forEach(function(error) {
60+
if (error.message) {
61+
$(error.element).closest('tr').addClass('has-error').find('.error').remove();
62+
$('<span class=error>').text(error.message).insertAfter(error.element);
63+
}
64+
65+
if (focus && error.element.focus) {
66+
error.element.focus();
67+
focus = false;
68+
}
69+
});
70+
}
71+
72+
function removeErrors(elem)
73+
{
74+
if ($(elem).is('form')) {
75+
$('.has-error', elem).removeClass('has-error');
76+
$('.error', elem).remove();
77+
} else {
78+
$(elem).closest('tr').removeClass('has-error').find('.error').remove();
79+
}
80+
}
81+
82+
Nette.showFormErrors = function(form, errors) {
83+
removeErrors(form);
84+
showErrors(errors, true);
85+
};
86+
87+
$(function() {
88+
$(':input').keypress(function() {
89+
removeErrors(this);
90+
});
91+
92+
$(':input').blur(function() {
93+
Nette.formErrors = [];
94+
Nette.validateControl(this);
95+
showErrors(Nette.formErrors);
96+
});
97+
});
98+
</script>
99+
100+
<h1>Nette Forms live validation example</h1>
101+
102+
<?php echo $form ?>
103+
104+
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer>

examples/manual-rendering.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
]);
3030

3131
$form->addText('email')
32-
->addCondition($form::FILLED)
33-
->addRule($form::EMAIL, 'Incorrect email address');
32+
->setRequired(FALSE)
33+
->addRule($form::EMAIL, 'Incorrect email address');
3434

3535
$form->addSubmit('submit');
3636

0 commit comments

Comments
 (0)