Permalink
Browse files

feature #27043 [Form][TwigBridge] Add help_attr (mpiot)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Form][TwigBridge] Add help_attr

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Add help_attr to the  form_help method.

Commits
-------

42d54b7 Add help_attr
  • Loading branch information...
fabpot committed Oct 17, 2018
2 parents 7048679 + 42d54b7 commit f0814e17d739856f060d7656082f963c08a0e92e
@@ -177,7 +177,9 @@
{% block form_help -%}
{%- if help is not empty -%}
<span id="{{ id }}_help" class="help-block">
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-block')|trim}) -%}
<span id="{{ id }}_help" {% for attrname, attrvalue in help_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{%- if translation_domain is same as(false) -%}
{{- help -}}
{%- else -%}
@@ -297,7 +297,9 @@
{% block form_help -%}
{%- if help is not empty -%}
<small id="{{ id }}_help" class="form-text text-muted">
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text text-muted')|trim}) -%}
<small id="{{ id }}_help" {% for attrname, attrvalue in help_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{%- if translation_domain is same as(false) -%}
{{- help -}}
{%- else -%}
@@ -291,7 +291,9 @@
{% block form_help -%}
{%- if help is not empty -%}
<p id="{{ id }}_help" class="help-text">
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-text')|trim}) -%}
<p id="{{ id }}_help" {% for attrname, attrvalue in help_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{%- if translation_domain is same as(false) -%}
{{- help -}}
{%- else -%}
@@ -120,6 +120,26 @@ public function testHelp()
);
}
public function testHelpAttr()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
'help' => 'Help text test!',
'help_attr' => array(
'class' => 'class-test',
),
));
$view = $form->createView();
$html = $this->renderHelp($view);
$this->assertMatchesXpath($html,
'/span
[@id="name_help"]
[@class="class-test help-block"]
[.="[trans]Help text test![/trans]"]
'
);
}
public function testErrors()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');
@@ -177,6 +177,26 @@ public function testHelp()
);
}
public function testHelpAttr()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
'help' => 'Help text test!',
'help_attr' => array(
'class' => 'class-test',
),
));
$view = $form->createView();
$html = $this->renderHelp($view);
$this->assertMatchesXpath($html,
'/small
[@id="name_help"]
[@class="class-test form-text text-muted"]
[.="[trans]Help text test![/trans]"]
'
);
}
public function testErrors()
{
$form = $this->factory->createNamed('name', TextType::class);
@@ -190,6 +190,26 @@ public function testMoneyWidgetInIso()
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
}
public function testHelpAttr()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
'help' => 'Help text test!',
'help_attr' => array(
'class' => 'class-test',
),
));
$view = $form->createView();
$html = $this->renderHelp($view);
$this->assertMatchesXpath($html,
'/p
[@id="name_help"]
[@class="class-test help-text"]
[.="[trans]Help text test![/trans]"]
'
);
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->renderer->renderBlock($view, 'form', $vars);
@@ -76,6 +76,26 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
$this->assertSame('<form name="form" method="get" action="0">', $html);
}
public function testHelpAttr()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
'help' => 'Help text test!',
'help_attr' => array(
'class' => 'class-test',
),
));
$view = $form->createView();
$html = $this->renderHelp($view);
$this->assertMatchesXpath($html,
'/p
[@id="name_help"]
[@class="class-test help-text"]
[.="[trans]Help text test![/trans]"]
'
);
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->renderer->renderBlock($view, 'form', $vars);
@@ -23,7 +23,7 @@
"symfony/asset": "~3.4|~4.0",
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/finder": "~3.4|~4.0",
"symfony/form": "^4.1.5",
"symfony/form": "^4.2",
"symfony/http-foundation": "~3.4|~4.0",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/polyfill-intl-icu": "~1.0",
@@ -42,7 +42,7 @@
},
"conflict": {
"symfony/console": "<3.4",
"symfony/form": "<4.1.2",
"symfony/form": "<4.2",
"symfony/translation": "<4.2"
},
"suggest": {
@@ -1,3 +1,4 @@
<?php if (!empty($help)): ?>
<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>
<?php $help_attr['class'] = isset($help_attr['class']) ? trim($help_attr['class'].' help-text') : 'help-text'; ?>
<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>
<?php endif; ?>
@@ -93,6 +93,26 @@ public function testMoneyWidgetInIso()
$this->assertSame('&euro; <input type="text" id="name" name="name" required="required" />', $this->renderWidget($view));
}
public function testHelpAttr()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
'help' => 'Help text test!',
'help_attr' => array(
'class' => 'class-test',
),
));
$view = $form->createView();
$html = $this->renderHelp($view);
$this->assertMatchesXpath($html,
'/p
[@id="name_help"]
[@class="class-test help-text"]
[.="[trans]Help text test![/trans]"]
'
);
}
protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->engine->get('form')->form($view, $vars);
@@ -51,6 +51,26 @@ public function testStartTagHasActionAttributeWhenActionIsZero()
$this->assertSame('<form name="form" method="get" action="0">', $html);
}
public function testHelpAttr()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
'help' => 'Help text test!',
'help_attr' => array(
'class' => 'class-test',
),
));
$view = $form->createView();
$html = $this->renderHelp($view);
$this->assertMatchesXpath($html,
'/p
[@id="name_help"]
[@class="class-test help-text"]
[.="[trans]Help text test![/trans]"]
'
);
}
protected function getExtensions()
{
// should be moved to the Form component once absolute file paths are supported
@@ -90,6 +90,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'size' => null,
'label_attr' => $options['label_attr'],
'help' => $options['help'],
'help_attr' => $options['help_attr'],
'compound' => $formConfig->getCompound(),
'method' => $formConfig->getMethod(),
'action' => $formConfig->getAction(),
@@ -180,11 +181,13 @@ public function configureOptions(OptionsResolver $resolver)
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
'upload_max_size_message' => $uploadMaxSizeMessage, // internal
'help' => null,
'help_attr' => array(),
));
$resolver->setAllowedTypes('label_attr', 'array');
$resolver->setAllowedTypes('upload_max_size_message', array('callable'));
$resolver->setAllowedTypes('help', array('string', 'null'));
$resolver->setAllowedTypes('help_attr', 'array');
}
/**
@@ -35,6 +35,7 @@
"data",
"disabled",
"help",
"help_attr",
"inherit_data",
"label",
"label_attr",
@@ -15,8 +15,9 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
expanded data
group_by disabled
multiple help
placeholder inherit_data
preferred_choices label
placeholder help_attr
preferred_choices inherit_data
label
label_attr
label_format
mapped
@@ -15,6 +15,7 @@
"empty_data",
"error_bubbling",
"help",
"help_attr",
"inherit_data",
"label",
"label_attr",
@@ -17,6 +17,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
empty_data
error_bubbling
help
help_attr
inherit_data
label
label_attr

0 comments on commit f0814e1

Please sign in to comment.