Skip to content

Commit

Permalink
Merge pull request #9 from ales-zrak/master
Browse files Browse the repository at this point in the history
Vertical form version (bootstrap default)
  • Loading branch information
tomaj committed Feb 7, 2016
2 parents ccb1c9a + ab809b4 commit 3d9c337
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ $form->setRenderer(new BootstrapRenderer);
```

For inline form you can use ```Tomaj\Form\Renderer\BootstrapInlineRenderer```

For vertical form (bootstrap default) you can use ```Tomaj\Form\Renderer\BootstrapVerticalRenderer```
89 changes: 89 additions & 0 deletions src/BootstrapVerticalRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Tomaj\Form\Renderer;

use Nette;
use Nette\Forms\Rendering\DefaultFormRenderer;
use Nette\Forms\Controls;

class BootstrapVerticalRenderer extends DefaultFormRenderer
{
public $wrappers = array(
'form' => array(
'container' => null,
),
'error' => array(
'container' => 'div class="alert alert-danger"',
'item' => 'p',
),
'group' => array(
'container' => 'fieldset',
'label' => 'legend',
'description' => 'p',
),
'controls' => array(
'container' => null,
),
'pair' => array(
'container' => 'div class=form-group',
'.required' => 'required',
'.optional' => null,
'.odd' => null,
'.error' => 'has-error',
),
'control' => array(
'container' => '',
'.odd' => null,
'description' => 'span class=help-block',
'requiredsuffix' => '',
'errorcontainer' => 'span class=help-block',
'erroritem' => '',
'.required' => 'required',
'.text' => 'text',
'.password' => 'text',
'.file' => 'text',
'.submit' => 'button',
'.image' => 'imagebutton',
'.button' => 'button',
),
'label' => array(
'container' => '',
'suffix' => null,
'requiredsuffix' => '',
),
'hidden' => array(
'container' => 'div',
),
);

/**
* Provides complete form rendering.
* @param Nette\Forms\Form
* @param string 'begin', 'errors', 'ownerrors', 'body', 'end' or empty to render all
* @return string
*/
public function render(Nette\Forms\Form $form, $mode = null)
{
$form->getElementPrototype()->setNovalidate('novalidate');

$usedPrimary = FALSE;
foreach ($form->getControls() as $control) {
if ($control instanceof Controls\Button) {
if (strpos($control->getControlPrototype()->getClass(), 'btn') === FALSE) {
$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);
}
}

0 comments on commit 3d9c337

Please sign in to comment.