Skip to content

Instantly share code, notes, and snippets.

@vojtabiberle
Created October 9, 2015 11:03
Show Gist options
  • Save vojtabiberle/6b8278a63707eea4b161 to your computer and use it in GitHub Desktop.
Save vojtabiberle/6b8278a63707eea4b161 to your computer and use it in GitHub Desktop.
Bootstrap 3 formuláře do Nette - namespace doplň podle podřeby
<?php
use Nette\ComponentModel\IContainer;
use Nette\Forms\Controls;
class Bootstrap3Form extends \Nette\Application\UI\Form
{
public function __construct(IContainer $parent = NULL, $name = NULL)
{
$this->setRenderer(new Bootstrap3FormRenderer());
}
}
<?php
use Nette\Forms\Form;
use Nette\Forms\Rendering\DefaultFormRenderer;
use Nette\Forms\Controls;
class Bootstrap3FormRenderer extends DefaultFormRenderer
{
public function __construct()
{
$this->wrappers['controls']['container'] = NULL;
$this->wrappers['pair']['container'] = 'div class=form-group';
$this->wrappers['pair']['.error'] = 'has-error';
$this->wrappers['control']['container'] = 'div class=col-sm-9';
$this->wrappers['label']['container'] = 'div class="col-sm-3 control-label"';
$this->wrappers['control']['description'] = 'span class=help-block';
$this->wrappers['control']['errorcontainer'] = 'span class=help-block';
}
public function render(Form $form, $mode = NULL)
{
$form->getElementPrototype()->class('form-horizontal');
foreach ($form->getControls() as $control) {
if ($control instanceof Controls\Button) {
$control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
$usedPrimary = TRUE;
} elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
$control->getControlPrototype()->addClass('form-control');
} elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
$control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
}
}
return parent::render($form, $mode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment