From ab809b4b08b9b9142cb5479419968fe0db8adafa Mon Sep 17 00:00:00 2001 From: basnik Date: Sat, 6 Feb 2016 20:19:56 +0100 Subject: [PATCH] Added vertical form version with labels and inputs on two lines, like default bootstrap rendering. Updated documentation. --- README.md | 2 + src/BootstrapVerticalRenderer.php | 89 +++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/BootstrapVerticalRenderer.php diff --git a/README.md b/README.md index 41aebfb..c4cd208 100644 --- a/README.md +++ b/README.md @@ -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``` diff --git a/src/BootstrapVerticalRenderer.php b/src/BootstrapVerticalRenderer.php new file mode 100644 index 0000000..cefe967 --- /dev/null +++ b/src/BootstrapVerticalRenderer.php @@ -0,0 +1,89 @@ + 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); + } +}