| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Nette Forms localization 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 |
class MyTranslator implements \Packetery\Nette\Localization\Translator |
| 17 |
{ |
| 18 |
private $table; |
| 19 |
public function __construct(array $table) |
| 20 |
{ |
| 21 |
$this->table = $table; |
| 22 |
} |
| 23 |
/** |
| 24 |
* Translates the given string. |
| 25 |
*/ |
| 26 |
public function translate($message, ...$parameters) : string |
| 27 |
{ |
| 28 |
return $this->table[$message] ?? $message; |
| 29 |
} |
| 30 |
} |
| 31 |
$form = new Form(); |
| 32 |
$translator = new MyTranslator(\parse_ini_file(__DIR__ . '/localization.ini')); |
| 33 |
$form->setTranslator($translator); |
| 34 |
$form->addGroup('Personal data'); |
| 35 |
$form->addText('name', 'Your name:')->setRequired('Enter your name'); |
| 36 |
$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]); |
| 37 |
$countries = ['World' => ['bu' => 'Buranda', 'qu' => 'Qumran', 'st' => 'Saint Georges Island'], '?' => 'other']; |
| 38 |
$form->addSelect('country', 'Country:', $countries)->setPrompt('Select your country'); |
| 39 |
$form->addSubmit('submit', 'Send'); |
| 40 |
if ($form->isSuccess()) { |
| 41 |
echo '<h2>Form was submitted and successfully validated</h2>'; |
| 42 |
Dumper::dump($form->getValues()); |
| 43 |
exit; |
| 44 |
} |
| 45 |
?> |
| 46 |
<!DOCTYPE html> |
| 47 |
<meta charset="utf-8"> |
| 48 |
<title>Nette Forms localization example</title> |
| 49 |
<link rel="stylesheet" media="screen" href="assets/style.css" /> |
| 50 |
<script src="https://nette.github.io/resources/js/3/netteForms.js"></script> |
| 51 |
|
| 52 |
<h1>Nette Forms localization example</h1> |
| 53 |
|
| 54 |
<?php |
| 55 |
$form->render(); |
| 56 |
?> |
| 57 |
|
| 58 |
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer> |
| 59 |
<?php |
| 60 |
|