Skip to content

Commit

Permalink
Added vertical form version with labels and inputs on two lines, like…
Browse files Browse the repository at this point in the history
… default bootstrap rendering.

Updated documentation.
  • Loading branch information
ales-zrak committed Feb 6, 2016
1 parent ccb1c9a commit ab809b4
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 ab809b4

Please sign in to comment.