| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* PacketeryNette Forms basic example. |
| 5 |
*/ |
| 6 |
|
| 7 |
declare(strict_types=1); |
| 8 |
|
| 9 |
|
| 10 |
if (@!include __DIR__ . '/../vendor/autoload.php') { |
| 11 |
die('Install packages using `composer install`'); |
| 12 |
} |
| 13 |
|
| 14 |
use PacketeryNette\Forms\Form; |
| 15 |
use PacketeryNette\Utils\Html; |
| 16 |
use PacketeryTracy\Debugger; |
| 17 |
use PacketeryTracy\Dumper; |
| 18 |
|
| 19 |
Debugger::enable(); |
| 20 |
|
| 21 |
|
| 22 |
$form = new Form; |
| 23 |
|
| 24 |
// group Personal data |
| 25 |
$form->addGroup('Personal data') |
| 26 |
->setOption('description', 'We value your privacy and we ensure that the information you give to us will not be shared to other entities.'); |
| 27 |
|
| 28 |
$form->addText('name', 'Your name:') |
| 29 |
->setRequired('Enter your name'); |
| 30 |
|
| 31 |
$form->addText('age', 'Your age:') |
| 32 |
->setRequired('Enter your age') |
| 33 |
->addRule($form::INTEGER, 'Age must be numeric value') |
| 34 |
->addRule($form::RANGE, 'Age must be in range from %d to %d', [10, 100]); |
| 35 |
|
| 36 |
$form->addRadioList('gender', 'Your gender:', [ |
| 37 |
'm' => 'male', |
| 38 |
'f' => 'female', |
| 39 |
]); |
| 40 |
|
| 41 |
$form->addCheckboxList('colors', 'Favorite colors:', [ |
| 42 |
'r' => 'red', |
| 43 |
'g' => 'green', |
| 44 |
'b' => 'blue', |
| 45 |
]); |
| 46 |
|
| 47 |
$form->addEmail('email', 'Email:') |
| 48 |
->setEmptyValue('@'); |
| 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 = [ |
| 71 |
'World' => [ |
| 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 |
->addRule($form::IMAGE, 'Uploaded file is not image'); |
| 97 |
|
| 98 |
$form->addHidden('userid'); |
| 99 |
|
| 100 |
$form->addTextArea('note', 'Comment:'); |
| 101 |
|
| 102 |
// group for buttons |
| 103 |
$form->addGroup(); |
| 104 |
|
| 105 |
$form->addSubmit('submit', 'Send'); |
| 106 |
|
| 107 |
|
| 108 |
$form->setDefaults([ |
| 109 |
'name' => 'John Doe', |
| 110 |
'userid' => 231, |
| 111 |
]); |
| 112 |
|
| 113 |
|
| 114 |
if ($form->isSuccess()) { |
| 115 |
echo '<h2>Form was submitted and successfully validated</h2>'; |
| 116 |
Dumper::dump($form->getValues(), [Dumper::COLLAPSE => false]); |
| 117 |
exit; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
?> |
| 122 |
<!DOCTYPE html> |
| 123 |
<meta charset="utf-8"> |
| 124 |
<title>PacketeryNette Forms basic example</title> |
| 125 |
<link rel="stylesheet" media="screen" href="assets/style.css" /> |
| 126 |
<script src="https://nette.github.io/resources/js/3/netteForms.js"></script> |
| 127 |
|
| 128 |
<h1>PacketeryNette Forms basic example</h1> |
| 129 |
|
| 130 |
<?php $form->render() ?> |
| 131 |
|
| 132 |
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer> |
| 133 |
|