Skip to content

[Form][TwigBridge] Add help_attr #27043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -42,7 +42,7 @@
},
"conflict": {
"symfony/console": "<3.4",
"symfony/form": "<4.1.2",
"symfony/form": "<4.2",
"symfony/translation": "<4.2"
},
"suggest": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"data",
"disabled",
"help",
"help_attr",
"inherit_data",
"label",
"label_attr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"empty_data",
"error_bubbling",
"help",
"help_attr",
"inherit_data",
"label",
"label_attr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down