PluginProbe
Packeta / trunk
Packeta vtrunk
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 / live-validation.php

live-validation.php in Packeta trunk, at deps/nette/forms/examples/live-validation.php

89 lines 2.6 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 live validation 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 $form = new Form();
17 $form->addText('name', 'Your name:')->setRequired('Enter your name');
18 $form->addText('age', 'Your age:')->setRequired('Enter your age')->addRule($form::INTEGER, 'Age must be numeric value')->addRule($form::RANGE, 'Age must be in range from %d to %d', [10, 100]);
19 $form->addPassword('password', 'Choose password:')->setRequired('Choose your password')->addRule($form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3);
20 $form->addPassword('password2', 'Reenter password:')->setRequired('Reenter your password')->addRule($form::EQUAL, 'Passwords do not match', $form['password']);
21 $form->addSubmit('submit', 'Send');
22 if ($form->isSuccess()) {
23 echo '<h2>Form was submitted and successfully validated</h2>';
24 Dumper::dump($form->getValues());
25 exit;
26 }
27 $renderer = $form->getRenderer();
28 $renderer->wrappers['pair']['.error'] = 'has-error';
29 ?>
30 <!DOCTYPE html>
31 <meta charset="utf-8">
32 <title>Nette Forms live validation example</title>
33 <link rel="stylesheet" media="screen" href="assets/style.css" />
34 <script src="https://nette.github.io/resources/js/3/netteForms.js"></script>
35 <script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
36
37 <script>
38 function showErrors(errors, focus)
39 {
40 errors.forEach(function(error) {
41 if (error.message) {
42 $(error.element).closest('tr').addClass('has-error').find('.error').remove();
43 $('<span class=error>').text(error.message).insertAfter(error.element);
44 }
45
46 if (focus && error.element.focus) {
47 error.element.focus();
48 focus = false;
49 }
50 });
51 }
52
53 function removeErrors(elem)
54 {
55 if ($(elem).is('form')) {
56 $('.has-error', elem).removeClass('has-error');
57 $('.error', elem).remove();
58 } else {
59 $(elem).closest('tr').removeClass('has-error').find('.error').remove();
60 }
61 }
62
63 Nette.showFormErrors = function(form, errors) {
64 removeErrors(form);
65 showErrors(errors, true);
66 };
67
68 $(function() {
69 $(':input').keypress(function() {
70 removeErrors(this);
71 });
72
73 $(':input').blur(function() {
74 Nette.formErrors = [];
75 Nette.validateControl(this);
76 showErrors(Nette.formErrors);
77 });
78 });
79 </script>
80
81 <h1>Nette Forms live validation example</h1>
82
83 <?php
84 $form->render();
85 ?>
86
87 <footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer>
88 <?php
89