Skip to content

[TwigBridge] Handle form label attributes like others #20365

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

Closed
wants to merge 2 commits into from
Closed

[TwigBridge] Handle form label attributes like others #20365

wants to merge 2 commits into from

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Oct 30, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

The HTML for rendering attributes is duplicated in multiple blocks, making it error prone/hard to maintain.

Next, the label attributes followed a different approach. Imo. all should follow the same base rendering, showing the above is actually an issue.

@keradus
Copy link
Member

keradus commented Oct 30, 2016

Something wrong happen with PR title

@ro0NL ro0NL changed the title [Form][Tw [Form] Consistent HTML attribute rendering Oct 30, 2016
@@ -1,10 +1,2 @@
<?php if (!empty($id)): ?>id="<?php echo $view->escape($id) ?>" <?php endif ?>
<?php foreach ($attr as $k => $v): ?>
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug :)

@@ -1,10 +1,2 @@
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($disabled): ?>disabled="disabled" <?php endif ?>
<?php foreach ($attr as $k => $v): ?>
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also bug :)

@HeahDude
Copy link
Contributor

HeahDude commented Nov 6, 2016

👍

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 27, 2016

@fabpot any chance to get this on the 3.2 train? It allows the label[title] attribute to be translated.. (being my field help text.. 😢 )

To clarify this is consistent with option[title] for the choice widget. see https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig#L82

@nicolas-grekas
Copy link
Member

There will be 3.2.1 after 3.2.0 :)

@ro0NL
Copy link
Contributor Author

ro0NL commented Nov 27, 2016

That is true 👍 the RC release triggered me that maybe this was forgotten / overlooked at 👼

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@ro0NL
Copy link
Contributor Author

ro0NL commented Dec 15, 2016

We work around it till 3.3 then :) the 2 bugs are real though.. but i should separate them for 2.7 i just realized. 👍

@@ -242,7 +242,7 @@
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
<label{% if label_attr %} {% set attr = label_attr %}{{ block('attributes') }}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use {% with %} around the block rendering instead of assigning label_attr to attr, which swallows the original attr variable and could cause issues

fabpot added a commit that referenced this pull request Dec 17, 2016
…ttributes (ro0NL)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle] Fix PHP form templates on translatable attributes

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20365 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Separated from #20365

Commits
-------

10806e0 [FrameworkBundle] Fix PHP form templates on translatable attributes
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Dec 17, 2016
…ttributes (ro0NL)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle] Fix PHP form templates on translatable attributes

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony#20365 (comment)
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Separated from #20365

Commits
-------

10806e0 [FrameworkBundle] Fix PHP form templates on translatable attributes
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 with minor comments

@@ -1 +1,9 @@
<?php echo $view['form']->block($form, 'widget_attributes') ?>
<?php foreach ($attr as $k => $v): ?>
<?php if (in_array($k, array('placeholder', 'title'), true)): ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth doing a micro optim here and turn this into:
if ('placeholder' === $k || 'title' === $k):

<?php foreach ($attr as $k => $v): ?>
<?php if (in_array($k, array('placeholder', 'title'), true)): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
<?php elseif ($v === true): ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yoda style (same below)

@ro0NL ro0NL changed the title [Form] Consistent HTML attribute rendering [TwigBridge] Handle label attributes like others Dec 29, 2016
@ro0NL ro0NL changed the title [TwigBridge] Handle label attributes like others [TwigBridge] Handle form label attributes like others Dec 29, 2016
@ro0NL
Copy link
Contributor Author

ro0NL commented Dec 29, 2016

Updated. I qualify it a feature, not a bug.

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

Thank you @ro0NL.

@fabpot fabpot closed this in 1cb9afd Mar 22, 2017
@ro0NL ro0NL deleted the form/attributes branch March 22, 2017 17:54
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
@fabpot fabpot mentioned this pull request May 1, 2017
nicolas-grekas added a commit that referenced this pull request May 23, 2017
…ributes (craue)

This PR was merged into the 3.3 branch.

Discussion
----------

[Form][3.3] avoid double blanks while rendering form attributes

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | kind of
| New feature?  | no
| BC breaks?    | no (rather reverts one)
| Deprecations? | no
| Tests pass?   | we'll see
| Fixed tickets | --
| License       | MIT
| Doc PR        | --

This fix avoids the double blanks introduced by #20365 when using the Twig template. The `attributes` block already renders one blank before each attribute, so there's no need to add another one prior to calling the block.

Commits
-------

a9c11c9 avoid double blanks while rendering form attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants