Skip to content

Commit 10e1a23

Browse files
committed
added Form renderers
1 parent c2e6208 commit 10e1a23

File tree

10 files changed

+426
-0
lines changed

10 files changed

+426
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gitattributes export-ignore
2+
.gitignore export-ignore
3+
.github export-ignore
4+
tests/ export-ignore
5+
*.sh eol=lf
6+
composer.json eol=lf

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/composer.lock
3+
/.idea

.phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
includes:
2+
- vendor/phpstan/phpstan-nette/extension.neon
3+
- vendor/phpstan/phpstan-nette/rules.neon

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
8+
matrix:
9+
fast_finish: true
10+
11+
cache:
12+
directories:
13+
- $HOME/.composer/cache
14+
15+
before_script:
16+
- composer install --no-interaction --prefer-source
17+
18+
script:
19+
- vendor/bin/phpstan analyze src -l 7 -c .phpstan.neon
20+
21+
after_failure:
22+
# Print *.actual content & log content
23+
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
24+
- for i in $(find tests -name \*.log); do echo "--- $i"; cat $i; echo; echo; done

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "nextras/forms-rendering",
3+
"type": "library",
4+
"description": "Rendering helpers for Nette Framework Forms.",
5+
"keywords": ["nette", "form", "forms", "rendering", "nextras"],
6+
"license": ["MIT"],
7+
"authors": [
8+
{
9+
"name": "Nextras Community",
10+
"homepage": "https://github.com/nextras/forms-rendering/graphs/contributors"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.1",
15+
"nette/forms": "~3.0",
16+
"nette/utils": "~3.0",
17+
"latte/latte": "~2.5"
18+
},
19+
"require-dev": {
20+
"nette/application": "~3.0",
21+
"nette/bootstrap": "~3.0",
22+
"nette/di": "~3.0",
23+
"nette/robot-loader": "~3.0",
24+
"phpstan/phpstan-nette": "0.11",
25+
"phpstan/phpstan-shim": "0.11.5",
26+
"tracy/tracy": "~2.5"
27+
},
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "1.0-dev"
31+
}
32+
},
33+
"config": {
34+
"sort-packages": true
35+
},
36+
"autoload": {
37+
"psr-4": { "Nextras\\FormsRendering\\": "src/" }
38+
}
39+
}

license.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MIT License
2+
===========
3+
4+
Copyright (c) 2019 Nextras Project
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Nextras Forms Rendering
2+
=======================
3+
4+
[![Build Status](https://travis-ci.org/nextras/forms-rendering.svg?branch=master)](https://travis-ci.org/nextras/forms-rendering)
5+
[![Downloads this Month](https://img.shields.io/packagist/dm/nextras/forms-rendering.svg?style=flat)](https://packagist.org/packages/nextras/forms-rendering)
6+
[![Stable version](http://img.shields.io/packagist/v/nextras/forms-rendering.svg?style=flat)](https://packagist.org/packages/nextras/forms-rendering)
7+
8+
This package provides rendering helpers Nette Forms.
9+
10+
Form renderers:
11+
- *Bs3Renderer* - renderer for Bootstrap 3 with horizontal mode only;
12+
- *Bs4Renderer* - renderer for Bootstrap 4 with support for horizontal, vertial and inline mode;
13+
14+
### Installation
15+
16+
The best way to install is using [Composer](http://getcomposer.org/):
17+
18+
```sh
19+
$ composer require nextras/forms-rendering
20+
```
21+
22+
### Documentation
23+
24+
See examples directory.
25+
26+
27+
### License
28+
29+
MIT. See full [license](license.md).

src/Renderers/Bs3FormRenderer.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* This file is part of the Nextras community extensions of Nette Framework
5+
*
6+
* @license MIT
7+
* @link https://github.com/nextras/forms-rendering
8+
*/
9+
10+
namespace Nextras\FormsRendering\Renderers;
11+
12+
use Nette\Forms\Controls;
13+
use Nette\Forms\Form;
14+
use Nette\Forms\IControl;
15+
use Nette\Forms\Rendering\DefaultFormRenderer;
16+
use Nette\Utils\Html;
17+
18+
19+
/**
20+
* Form renderer for Bootstrap 3.
21+
*/
22+
class Bs3FormRenderer extends DefaultFormRenderer
23+
{
24+
/** @var Controls\Button */
25+
public $primaryButton = null;
26+
27+
/** @var bool */
28+
private $controlsInit = false;
29+
30+
31+
public function __construct()
32+
{
33+
$this->wrappers['controls']['container'] = null;
34+
$this->wrappers['pair']['container'] = 'div class=form-group';
35+
$this->wrappers['pair']['.error'] = 'has-error';
36+
$this->wrappers['control']['container'] = 'div class=col-sm-9';
37+
$this->wrappers['label']['container'] = 'div class="col-sm-3 control-label"';
38+
$this->wrappers['control']['description'] = 'span class=help-block';
39+
$this->wrappers['control']['errorcontainer'] = 'span class=help-block';
40+
$this->wrappers['error']['container'] = null;
41+
$this->wrappers['error']['item'] = 'div class="alert alert-danger"';
42+
}
43+
44+
45+
public function renderBegin(): string
46+
{
47+
$this->controlsInit();
48+
return parent::renderBegin();
49+
}
50+
51+
52+
public function renderEnd(): string
53+
{
54+
$this->controlsInit();
55+
return parent::renderEnd();
56+
}
57+
58+
59+
public function renderBody(): string
60+
{
61+
$this->controlsInit();
62+
return parent::renderBody();
63+
}
64+
65+
66+
public function renderControls($parent): string
67+
{
68+
$this->controlsInit();
69+
return parent::renderControls($parent);
70+
}
71+
72+
73+
public function renderPair(IControl $control): string
74+
{
75+
$this->controlsInit();
76+
return parent::renderPair($control);
77+
}
78+
79+
80+
public function renderPairMulti(array $controls): string
81+
{
82+
$this->controlsInit();
83+
return parent::renderPairMulti($controls);
84+
}
85+
86+
87+
public function renderLabel(IControl $control): Html
88+
{
89+
$this->controlsInit();
90+
return parent::renderLabel($control);
91+
}
92+
93+
94+
public function renderControl(IControl $control): Html
95+
{
96+
$this->controlsInit();
97+
return parent::renderControl($control);
98+
}
99+
100+
101+
private function controlsInit()
102+
{
103+
if ($this->controlsInit) {
104+
return;
105+
}
106+
107+
$this->controlsInit = true;
108+
$this->form->getElementPrototype()->addClass('form-horizontal');
109+
foreach ($this->form->getControls() as $control) {
110+
if ($control instanceof Controls\Button) {
111+
$markAsPrimary = $control === $this->primaryButton || (!isset($this->primaryButton) && empty($usedPrimary) && $control->parent instanceof Form);
112+
if ($markAsPrimary) {
113+
$class = 'btn btn-primary';
114+
$usedPrimary = true;
115+
} else {
116+
$class = 'btn btn-default';
117+
}
118+
$control->getControlPrototype()->addClass($class);
119+
} elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
120+
$control->getControlPrototype()->addClass('form-control');
121+
} elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
122+
if ($control instanceof Controls\Checkbox) {
123+
$control->getSeparatorPrototype()->setName('div')->appendAttribute('class', $control->getControlPrototype()->type);
124+
} else {
125+
$control->getItemLabelPrototype()->addClass($control->getControlPrototype()->type . '-inline');
126+
}
127+
}
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)