8000 Add help_attr · symfony/symfony@42d54b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 42d54b7

Browse files
mpiotxabbuh
authored andcommitted
Add help_attr
1 parent 557f85d commit 42d54b7

16 files changed

+142
-8
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@
177177

178178
{% block form_help -%}
179179
{%- if help is not empty -%}
180-
<span id="{{ id }}_help" class="help-block">
180+
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-block')|trim}) -%}
181+
182+
<span id="{{ id }}_help" {% for attrname, attrvalue in help_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
181183
{%- if translation_domain is same as(false) -%}
182184
{{- help -}}
183185
{%- else -%}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@
297297

298298
{% block form_help -%}
299299
{%- if help is not empty -%}
300-
<small id="{{ id }}_help" class="form-text text-muted">
300+
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text text-muted')|trim}) -%}
301+
302+
<small id="{{ id }}_help" {% for attrname, attrvalue in help_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
301303
{%- if translation_domain is same as(false) -%}
302304
{{- help -}}
303305
{%- else -%}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@
291291

292292
{% block form_help -%}
293293
{%- if help is not empty -%}
294-
<p id="{{ id }}_help" class="help-text">
294+
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-text')|trim}) -%}
295+
296+
<p id="{{ id }}_help" {% for attrname, attrvalue in help_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
295297
{%- if translation_domain is same as(false) -%}
296298
{{- help -}}
297299
{%- else -%}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ public function testHelp()
120120
);
121121
}
122122

123+
public function testHelpAttr()
124+
{
125+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
126+
'help' => 'Help text test!',
127+
'help_attr' => array(
128+
'class' => 'class-test',
129+
),
130+
));
131+
$view = $form->createView();
132+
$html = $this->renderHelp($view);
133+
134+
$this->assertMatchesXpath($html,
135+
'/span
136+
[@id="name_help"]
137+
[@class="class-test help-block"]
138+
[.="[trans]Help text test![/trans]"]
139+
'
140+
);
141+
}
142+
123143
public function testErrors()
124144
{
125145
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ public function testHelp()
177177
);
178178
}
179179

180+
public function testHelpAttr()
181+
{
182+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
183+
'help' => 'Help text test!',
184+
'help_attr' => array(
185+
'class' => 'class-test',
186+
),
187+
));
188+
$view = $form->createView();
189+
$html = $this->renderHelp($view);
190+
191+
$this->assertMatchesXpath($html,
192+
'/small
193+
[@id="name_help"]
194+
[@class="class-test form-text text-muted"]
195+
[.="[trans]Help text test![/trans]"]
196+
'
197+
);
198+
}
199+
180200
public function testErrors()
181201
{
182202
$form = $this->factory->createNamed('name', TextType::class);

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,26 @@ public function testMoneyWidgetInIso()
190190
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
191191
}
192192

193+
public function testHelpAttr()
194+
{
195+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
196+
'help' => 'Help text test!',
197+
'help_attr' => array(
198+
'class' => 'class-test',
199+
),
200+
));
201+
$view = $form->createView();
202+
$html = $this->renderHelp($view);
203+
204+
$this->assertMatchesXpath($html,
205+
'/p
206+
[@id="name_help"]
207+
[@class="class-test help-text"]
208+
[.="[trans]Help text test![/trans]"]
209+
'
210+
);
211+
}
212+
193213
protected function renderForm(FormView $view, array $vars = array())
194214
{
195215
return (string) $this->renderer->renderBlock($view, 'form', $vars);

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
7676
$this->assertSame('<form name="form" method="get" action="0">', $html);
7777
}
7878

79+
public function testHelpAttr()
80+
{
81+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
82+
'help' => 'Help text test!',
83+
'help_attr' => array(
84+
'class' => 'class-test',
85+
),
86+
));
87+
$view = $form->createView();
88+
$html = $this->renderHelp($view);
89+
90+
$this->assertMatchesXpath($html,
91+
'/p
92+
[@id="name_help"]
93+
[@class="class-test help-text"]
94+
[.="[trans]Help text test![/trans]"]
95+
'
96+
);
97+
}
98+
7999
protected function renderForm(FormView $view, array $vars = array())
80100
{
81101
return (string) $this->renderer->renderBlock($view, 'form', $vars);

src/Symfony/Bridge/Twig/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"symfony/asset": "~3.4|~4.0",
2424
"symfony/dependency-injection": "~3.4|~4.0",
2525
"symfony/finder": "~3.4|~4.0",
26-
"symfony/form": "^4.1.5",
26+
"symfony/form": "^4.2",
2727
"symfony/http-foundation": "~3.4|~4.0",
2828
"symfony/http-kernel": "~3.4|~4.0",
2929
"symfony/polyfill-intl-icu": "~1.0",
@@ -42,7 +42,7 @@
4242
},
4343
"conflict": {
4444
"symfony/console": "<3.4",
45-
"symfony/form": "<4.1.2",
45+
"symfony/form": "<4.2",
4646
"symfony/translation": "<4.2"
4747
},
4848
"suggest": {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php if (!empty($help)): ?>
2-
<p id="<?php echo $view->escape($id); ?>_help" class="help-text"><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($help, array(), $translation_domain) : $help); ?></p>
2+
<?php $help_attr['class'] = isset($help_attr['class']) ? trim($help_attr['class'].' help-text') : 'help-text'; ?>
3+
<p id="<?php echo $view->escape($id); ?>_help" <?php echo ' '.$view['form']->block($form, 'attributes', array('attr' => $help_attr)); ?>><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($help, array(), $translation_domain) : $help); ?></p>
34
<?php endif; ?>

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ public function testMoneyWidgetInIso()
9393
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
9494
}
9595

96+
public function testHelpAttr()
97+
{
98+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
99+
'help' => 'Help text test!',
100+
'help_attr' => array(
101+
'class' => 'class-test',
102+
),
103+
));
104+
$view = $form->createView();
105+
$html = $this->renderHelp($view);
106+
107+
$this->assertMatchesXpath($html,
108+
'/p
109+
[@id="name_help"]
110+
[@class="class-test help-text"]
111+
[.="[trans]Help text test![/trans]"]
112+
'
113+
);
114+
}
115+
96116
protected function renderForm(FormView $view, array $vars = array())
97117
{
98118
return (string) $this->engine->get('form')->form($view, $vars);

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
5151
$this->assertSame('<form name="form" method="get" action="0">', $html);
5252
}
5353

54+
public function testHelpAttr()
55+
{
56+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
57+
'help' => 'Help text test!',
58+
'help_attr' => array(
59+
'class' => 'class-test',
60+
),
61+
));
62+
$view = $form->createView();
63+
$html = $this->renderHelp($view);
64+
65+
$this->assertMatchesXpath($html,
66+
'/p
67+
[@id="name_help"]
68+
[@class="class-test help-text"]
69+
[.="[trans]Help text test![/trans]"]
70+
'
71+
);
72+
}
73+
5474
protected function getExtensions()
5575
{
5676
// should be moved to the Form component once absolute file paths are supported

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
9090
'size' => null,
9191
'label_attr' => $options['label_attr'],
9292
'help' => $options['help'],
93+
'help_attr' => $options['help_attr'],
9394
'compound' => $formConfig->getCompound(), 10000
9495
'method' => $formConfig->getMethod(),
9596
'action' => $formConfig->getAction(),
@@ -180,11 +181,13 @@ public function configureOptions(OptionsResolver $resolver)
180181
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
181182
'upload_max_size_message' => $uploadMaxSizeMessage, // internal
182183
'help' => null,
184+
'help_attr' => array(),
183185
));
184186

185187
$resolver->setAllowedTypes('label_attr', 'array');
186188
$resolver->setAllowedTypes('upload_max_size_message', array('callable'));
187189
$resolver->setAllowedTypes('help', array('string', 'null'));
190+
$resolver->setAllowedTypes('help_attr', 'array');
188191
}
189192

190193
/**

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"data",
3636
"disabled",
3737
"help",
38+
"help_attr",
3839
"inherit_data",
3940
"label",
4041
"label_attr",

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
1515
expanded data
1616
group_by disabled
1717
multiple help
18-
placeholder inherit_data
19-
preferred_choices label
18+
placeholder help_attr
19+
preferred_choices inherit_data
20+
label
2021
label_attr
2122
label_format
2223
mapped

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"empty_data",
1616
"error_bubbling",
1717
"help",
18+
"help_attr",
1819
"inherit_data",
1920
"label",
2021
"label_attr",

src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
1717
empty_data
1818
error_bubbling
1919
help
20+
help_attr
2021
inherit_data
2122
label
2223
label_attr

0 commit comments

Comments
 (0)
0