| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Nette Forms & Bootstap v3 rendering 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 |
function makeBootstrap3(Form $form) : void |
| 17 |
{ |
| 18 |
$renderer = $form->getRenderer(); |
| 19 |
$renderer->wrappers['controls']['container'] = null; |
| 20 |
$renderer->wrappers['pair']['container'] = 'div class=form-group'; |
| 21 |
$renderer->wrappers['pair']['.error'] = 'has-error'; |
| 22 |
$renderer->wrappers['control']['container'] = 'div class=col-sm-9'; |
| 23 |
$renderer->wrappers['label']['container'] = 'div class="col-sm-3 control-label"'; |
| 24 |
$renderer->wrappers['control']['description'] = 'span class=help-block'; |
| 25 |
$renderer->wrappers['control']['errorcontainer'] = 'span class=help-block'; |
| 26 |
$form->getElementPrototype()->class('form-horizontal'); |
| 27 |
foreach ($form->getControls() as $control) { |
| 28 |
$type = $control->getOption('type'); |
| 29 |
if ($type === 'button') { |
| 30 |
$control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default'); |
| 31 |
$usedPrimary = \true; |
| 32 |
} elseif (\in_array($type, ['text', 'textarea', 'select'], \true)) { |
| 33 |
$control->getControlPrototype()->addClass('form-control'); |
| 34 |
} elseif (\in_array($type, ['checkbox', 'radio'], \true)) { |
| 35 |
$control->getSeparatorPrototype()->setName('div')->addClass($type); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
$form = new Form(); |
| 40 |
$form->onRender[] = 'makeBootstrap3'; |
| 41 |
$form->addGroup('Personal data'); |
| 42 |
$form->addText('name', 'Your name')->setRequired('Enter your name'); |
| 43 |
$form->addRadioList('gender', 'Your gender', ['male', 'female']); |
| 44 |
$form->addCheckboxList('colors', 'Favorite colors', ['red', 'green', 'blue']); |
| 45 |
$form->addSelect('country', 'Country', ['Buranda', 'Qumran', 'Saint Georges Island']); |
| 46 |
$form->addCheckbox('send', 'Ship to address'); |
| 47 |
$form->addGroup('Your account'); |
| 48 |
$form->addPassword('password', 'Choose password'); |
| 49 |
$form->addUpload('avatar', 'Picture'); |
| 50 |
$form->addTextArea('note', 'Comment'); |
| 51 |
$form->addGroup(); |
| 52 |
$form->addSubmit('submit', 'Send'); |
| 53 |
$form->addSubmit('cancel', 'Cancel'); |
| 54 |
if ($form->isSuccess()) { |
| 55 |
echo '<h2>Form was submitted and successfully validated</h2>'; |
| 56 |
Dumper::dump($form->getValues()); |
| 57 |
exit; |
| 58 |
} |
| 59 |
?> |
| 60 |
<!DOCTYPE html> |
| 61 |
<meta charset="utf-8"> |
| 62 |
<title>Nette Forms & Bootstrap v3 rendering example</title> |
| 63 |
|
| 64 |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> |
| 65 |
|
| 66 |
<div class="container"> |
| 67 |
<div class="page-header"> |
| 68 |
<h1>Nette Forms & Bootstrap v3 rendering example</h1> |
| 69 |
</div> |
| 70 |
|
| 71 |
<?php |
| 72 |
$form->render(); |
| 73 |
?> |
| 74 |
</div> |
| 75 |
<?php |
| 76 |
|