| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* PacketeryNette Forms localization 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 |
|
| 15 |
use PacketeryNette\Forms\Form; |
| 16 |
use PacketeryTracy\Debugger; |
| 17 |
use PacketeryTracy\Dumper; |
| 18 |
|
| 19 |
Debugger::enable(); |
| 20 |
|
| 21 |
|
| 22 |
class MyTranslator implements PacketeryNette\Localization\Translator |
| 23 |
{ |
| 24 |
private $table; |
| 25 |
|
| 26 |
|
| 27 |
public function __construct(array $table) |
| 28 |
{ |
| 29 |
$this->table = $table; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Translates the given string. |
| 35 |
*/ |
| 36 |
public function translate($message, ...$parameters): string |
| 37 |
{ |
| 38 |
return $this->table[$message] ?? $message; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
$form = new Form; |
| 44 |
|
| 45 |
$translator = new MyTranslator(parse_ini_file(__DIR__ . '/localization.ini')); |
| 46 |
$form->setTranslator($translator); |
| 47 |
|
| 48 |
$form->addGroup('Personal data'); |
| 49 |
$form->addText('name', 'Your name:') |
| 50 |
->setRequired('Enter your name'); |
| 51 |
|
| 52 |
$form->addText('age', 'Your age:') |
| 53 |
->setRequired('Enter your age') |
| 54 |
->addRule($form::INTEGER, 'Age must be numeric value') |
| 55 |
->addRule($form::RANGE, 'Age must be in range from %d to %d', [10, 100]); |
| 56 |
|
| 57 |
$countries = [ |
| 58 |
'World' => [ |
| 59 |
'bu' => 'Buranda', |
| 60 |
'qu' => 'Qumran', |
| 61 |
'st' => 'Saint Georges Island', |
| 62 |
], |
| 63 |
'?' => 'other', |
| 64 |
]; |
| 65 |
$form->addSelect('country', 'Country:', $countries) |
| 66 |
->setPrompt('Select your country'); |
| 67 |
|
| 68 |
$form->addSubmit('submit', 'Send'); |
| 69 |
|
| 70 |
|
| 71 |
if ($form->isSuccess()) { |
| 72 |
echo '<h2>Form was submitted and successfully validated</h2>'; |
| 73 |
Dumper::dump($form->getValues()); |
| 74 |
exit; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
?> |
| 79 |
<!DOCTYPE html> |
| 80 |
<meta charset="utf-8"> |
| 81 |
<title>PacketeryNette Forms localization example</title> |
| 82 |
<link rel="stylesheet" media="screen" href="assets/style.css" /> |
| 83 |
<script src="https://nette.github.io/resources/js/3/netteForms.js"></script> |
| 84 |
|
| 85 |
<h1>PacketeryNette Forms localization example</h1> |
| 86 |
|
| 87 |
<?php $form->render() ?> |
| 88 |
|
| 89 |
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer> |
| 90 |
|