Skip to content

Commit d500b49

Browse files
authored
Merge pull request #18 from dipcode-software/bugfix/bootstrap4-class-ignored
Fixed form field error class being lost
2 parents 1b9c910 + 34b9224 commit d500b49

File tree

7 files changed

+57
-7
lines changed

7 files changed

+57
-7
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [2.0.3] - 2017-12-13
8+
### Added
9+
- `setRequired` to `Field` class;
10+
- Twig Filter `merge_str` to merge value into as array value through implode.
11+
12+
### Fixed
13+
- Form field error class being ignored in `Bootstrap4TemplatePack`.
14+
715
## [2.0.2] - 2017-12-12
816
### Changed
917
- `Twig` minimal version supported setted to `>=1.35`.

src/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Config extends Singleton
99
{
10-
const VERSION = "v2.0.0";
10+
const VERSION = "v2.0.3";
1111

1212
/**
1313
* @var array Template packs to be used. The templates will be loaded

src/Renderers/TwigRenderer.php

+24-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
namespace PHPForm\Renderers;
66

7-
use Twig\Loader\FilesystemLoader;
8-
use Twig\Environment;
97
use Twig\Loader\ChainLoader;
8+
use Twig\Environment;
9+
use Twig\Loader\FilesystemLoader;
10+
use Twig\TwigFilter;
1011

1112
class TwigRenderer implements Renderer
1213
{
@@ -22,15 +23,34 @@ class TwigRenderer implements Renderer
2223
*
2324
* @param array $template_dirs
2425
*/
25-
public function __construct(array $templates_dirs)
26+
public function __construct(array $templates_dirs, array $options = array())
2627
{
2728
$loaders = new ChainLoader();
2829

2930
foreach ($templates_dirs as $template_dir) {
3031
$loaders->addLoader(new FilesystemLoader($template_dir));
3132
}
3233

33-
$this->twig = new Environment($loaders);
34+
$this->twig = new Environment($loaders, $options);
35+
$this->setFilters();
36+
}
37+
38+
public function setFilters()
39+
{
40+
$filter_merge_str = new TwigFilter('merge_str', function ($attrs, array $options = array()) {
41+
$key = $options[0];
42+
$value = $options[1];
43+
44+
if (array_key_exists($key, $attrs)) {
45+
$attrs[$key] = implode(' ', [$value, $attrs[$key]]);
46+
} else {
47+
$attrs[$key] = $value;
48+
}
49+
50+
return $attrs;
51+
}, array('is_variadic' => true));
52+
53+
$this->twig->addFilter($filter_merge_str);
3454
}
3555

3656
/**
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<label class="custom-file">
2-
{% set attrs = attrs|merge({'class': 'custom-file-input'}) %}
2+
{% set attrs = attrs|merge_str("class", "custom-file-input") %}
33
{% include 'input.html' %}
44
<span class="custom-file-control"></span>
55
</label>

src/TemplatePacks/templates/bootstrap4/multiple_input_option.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<label for="{{for|e}}" class="custom-control custom-{{ type|e }}">
2-
{% set attrs = attrs|merge({'class': 'custom-control-input'}) %}
2+
{% set attrs = attrs|merge_str("class", "custom-control-input") %}
33
{% include 'input.html' %}
44
<span class="custom-control-indicator"></span>
55
<span class="custom-control-description">{{ label|e }}</span>

tests/unit/Renderers/TwigRendererTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,24 @@ public function testRenderWithOnlyFallbackDefined()
3838

3939
$this->assertEquals($expected, $result);
4040
}
41+
42+
public function testFilterRenderStr()
43+
{
44+
$renderer = new TwigRenderer([__DIR__ . "/templates/pack1"]);
45+
46+
$result = $renderer->render("merge_str_filter.html", array("attrs" => ["class" => "existent"]));
47+
$expected = "class merged existent";
48+
49+
$this->assertEquals($expected, $result);
50+
}
51+
52+
public function testFilterRenderStrWithoutArg()
53+
{
54+
$renderer = new TwigRenderer([__DIR__ . "/templates/pack1"]);
55+
56+
$result = $renderer->render("merge_str_filter.html", array("attrs" => []));
57+
$expected = "class merged";
58+
59+
$this->assertEquals($expected, $result);
60+
}
4161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% set attrs = attrs|merge_str("class", "class merged") %}
2+
{{ attrs['class'] }}

0 commit comments

Comments
 (0)