Created
October 9, 2015 11:03
-
-
Save vojtabiberle/6b8278a63707eea4b161 to your computer and use it in GitHub Desktop.
Bootstrap 3 formuláře do Nette - namespace doplň podle podřeby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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