| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Nette Forms basic 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\Nette\Utils\Html; |
| 14 |
use Packetery\Tracy\Debugger; |
| 15 |
use Packetery\Tracy\Dumper; |
| 16 |
Debugger::enable(); |
| 17 |
$form = new Form(); |
| 18 |
// group Personal data |
| 19 |
$form->addGroup('Personal data')->setOption('description', 'We value your privacy and we ensure that the information you give to us will not be shared to other entities.'); |
| 20 |
$form->addText('name', 'Your name:')->setRequired('Enter your name'); |
| 21 |
$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]); |
| 22 |
$form->addRadioList('gender', 'Your gender:', ['m' => 'male', 'f' => 'female']); |
| 23 |
$form->addCheckboxList('colors', 'Favorite colors:', ['r' => 'red', 'g' => 'green', 'b' => 'blue']); |
| 24 |
$form->addEmail('email', 'Email:')->setEmptyValue('@'); |
| 25 |
// group Shipping address |
| 26 |
$form->addGroup('Shipping address')->setOption('embedNext', \true); |
| 27 |
$form->addCheckbox('send', 'Ship to address')->addCondition($form::FILLED)->toggle('sendBox'); |
| 28 |
// toggle div #sendBox |
| 29 |
// subgroup |
| 30 |
$form->addGroup()->setOption('container', Html::el('div')->id('sendBox')); |
| 31 |
$form->addText('street', 'Street:'); |
| 32 |
$form->addText('city', 'City:')->addConditionOn($form['send'], $form::FILLED)->setRequired('Enter your shipping address'); |
| 33 |
$countries = ['World' => ['bu' => 'Buranda', 'qu' => 'Qumran', 'st' => 'Saint Georges Island'], '?' => 'other']; |
| 34 |
$form->addSelect('country', 'Country:', $countries)->setPrompt('Select your country')->addConditionOn($form['send'], $form::FILLED)->setRequired('Select your country'); |
| 35 |
// group Your account |
| 36 |
$form->addGroup('Your account'); |
| 37 |
$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); |
| 38 |
$form->addPassword('password2', 'Reenter password:')->setRequired('Reenter your password')->addRule($form::EQUAL, 'Passwords do not match', $form['password']); |
| 39 |
$form->addUpload('avatar', 'Picture:')->addRule($form::IMAGE, 'Uploaded file is not image'); |
| 40 |
$form->addHidden('userid'); |
| 41 |
$form->addTextArea('note', 'Comment:'); |
| 42 |
// group for buttons |
| 43 |
$form->addGroup(); |
| 44 |
$form->addSubmit('submit', 'Send'); |
| 45 |
$form->setDefaults(['name' => 'John Doe', 'userid' => 231]); |
| 46 |
if ($form->isSuccess()) { |
| 47 |
echo '<h2>Form was submitted and successfully validated</h2>'; |
| 48 |
Dumper::dump($form->getValues(), [Dumper::COLLAPSE => \false]); |
| 49 |
exit; |
| 50 |
} |
| 51 |
?> |
| 52 |
<!DOCTYPE html> |
| 53 |
<meta charset="utf-8"> |
| 54 |
<title>Nette Forms basic example</title> |
| 55 |
<link rel="stylesheet" media="screen" href="assets/style.css" /> |
| 56 |
<script src="https://nette.github.io/resources/js/3/netteForms.js"></script> |
| 57 |
|
| 58 |
<h1>Nette Forms basic example</h1> |
| 59 |
|
| 60 |
<?php |
| 61 |
$form->render(); |
| 62 |
?> |
| 63 |
|
| 64 |
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer> |
| 65 |
<?php |
| 66 |
|