Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jaromirnyklicek/2039059 to your computer and use it in GitHub Desktop.
Save jaromirnyklicek/2039059 to your computer and use it in GitHub Desktop.
Changes Nette form default markup to be compatible with Bootstrap CSS framework.
<?php
namespace SmartCMS;
use Nette,
Nette\Utils\Html;
class BootstrapHorizontalFormRenderer extends Nette\Forms\Rendering\DefaultFormRenderer
{
public $wrappers = array(
'form' => array(
'container' => 'div class=form-horizontal',
'errors' => TRUE,
),
'error' => array(
'container' => 'ul class=error',
'item' => 'li',
),
'group' => array(
'container' => 'fieldset',
'label' => 'legend',
'description' => 'p',
),
'controls' => array(
'container' => NULL,
),
'pair' => array(
'container' => 'div class=control-group',
'.required' => 'required',
'.optional' => NULL,
'.odd' => NULL,
),
'control' => array(
'container' => 'div class=controls',
'.odd' => NULL,
'errors' => FALSE,
'description' => 'p class=help-block',
'requiredsuffix' => '',
'.required' => 'required',
'.text' => 'text',
'.password' => 'text',
'.file' => 'text',
'.submit' => 'button',
'.image' => 'imagebutton',
'.button' => 'button',
),
'label' => array(
'container' => NULL,
'suffix' => NULL,
'requiredsuffix' => '',
'.class' => 'control-label',
),
'hidden' => array(
'container' => 'div',
),
'buttons' => array(
'container' => 'div class=form-actions',
'.class' => 'btn'
),
);
public function renderLabel(Nette\Forms\IControl $control)
{
$head = $this->getWrapper('label container');
if ($control instanceof Nette\Forms\Controls\Checkbox || $control instanceof Nette\Forms\Controls\Button) {
return $head->setHtml(($head->getName() === 'td' || $head->getName() === 'th') ? '&nbsp;' : '');
} else {
$label = $control->getLabel();
$suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
$class = $this->getValue('label .class');
if ($label instanceof Html) {
$label->setHtml($label->getHtml() . $suffix);
$label->class($class, TRUE);
$suffix = '';
}
return $head->setHtml((string) $label . $suffix);
}
}
public function renderPairMulti(array $controls)
{
$s = array();
foreach ($controls as $control) {
if (!$control instanceof Nette\Forms\IControl) {
throw new Nette\InvalidArgumentException("Argument must be array of IFormControl instances.");
}
$controlPrototype = $control->getControl();
if($control instanceof Nette\Forms\Controls\Button) {
$class = $this->getValue('buttons .class');
$controlPrototype->class($class, TRUE);
}
$s[] = (string) $controlPrototype;
}
$pair = $this->getWrapper('buttons container');
$pair->add($this->renderLabel($control))->setHtml(implode(" ", $s));
return $pair->render(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment