-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Do not put any empty div if label is disabled #19247
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
Do not put any empty div if label is disabled #19247
Conversation
ae3f3e1
to
aeb4de6
Compare
{% else %} | ||
col-sm-10 | ||
{% endif %} | ||
{% endspaceless %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use whitespace control modifiers instead of the spaceless tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jvasseur fixed.
Prettier than #18504, looks great! Maybe a test would be good. |
This looks cleaner than having an empty div. |
@HeahDude Do we have test suites on Symfony for templates? I would be glad to add some. Could you please indicate where to go? |
aeb4de6
to
e4699e2
Compare
@@ -77,5 +75,9 @@ col-sm-2 | |||
{% endblock reset_row %} | |||
|
|||
{% block form_group_class -%} | |||
col-sm-10 | |||
{% if label is same as(false) -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't there a need for a minus sign before the if
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, there isn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, just checking 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed for white-space control.
If I remove it, I get:
<div class=" col-sm-12">
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you mean for block
! Indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok you're right @greg0ire, should be:
{%- block form_group_class -%}
{%- if label is same as(false) -%}
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure ? minus for both block
and if
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this happens in a string not at the start of a block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok… anyway I think @soullivaneuh checks what this produces, so…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did. And the actual state works correctly.
There is also https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php but it should not be concerned here. Maybe it is :) |
@HeahDude I added a test. Please tell me if it looks OK. |
830937e
to
66000e2
Compare
It looks good to me. But if you can attach screenshot before / after in the PR description, it would be very nice. |
$form = $this->factory->createNamed('name', 'date', null, array('label' => false)); | ||
$view = $form->createView(); | ||
$this->renderWidget($view); | ||
$html = $this->renderLabel($view); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider not using the $form
and $html
OTVs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be inlined in assertEmpty()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just a copy paste of a similar test.
@lyrixx I updated the PR body. 👍 Note: We can have one edge case AFAIK: ->add('test', TextType::class, [
'mapped' => false,
'label' => false,
'attr' => [
'placeholder' => 'My label goes here',
]
]) Renders: I think this case is very rare comparing to the other, but can still happen. Maybe could we have an |
col-sm-10 | ||
{%- endblock form_group_class %} | ||
{% block form_group_class %} | ||
{% if label is same as(false) -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing whitespace control here to remove the indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean {%- if
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, good catch!
Also maybe we should create a new block for the form-group class? Because ATM, user will have to copy/paste the whole logic if they want to change it: {% block form_group_class %}
{% if label is not same as(false) -%}
col-md-9
{%- else -%}
col-md-12
{%- endif %}
{% endblock form_group_class %} Or maybe a |
My two cents..
Can confirm the form I used in my pr looks the same with this code. 👍 |
The Consider this as a row for a form. AFAIK, we can make them nested.
Please see #19247 (comment). |
We're solving two different thing here. Collections and nested forms. Consider my nested form type the Form from my pr <div class="form-group">
<div class="col-sm-12">
<div id="foo_foo2">
<div class="form-group">
<label class="col-sm-2 control-label required" for="foo_foo2_child">Child</label>
<div class="col-sm-10">
<input type="text" id="foo_foo2_child" name="foo[foo2][child]" required="required" class="form-control" />
</div>
</div>
</div>
</div>
</div> When rendering the collection type I see the need for it. Is there a way to know the difference between them? Regarding BC-breaks. Yes, that comment is what I was thinking of. I don't think we should change the behavior and render the input the whole width. |
You mean about: <div class="form-group">
<div class="col-sm-12"> ? I don't see the issue. This is like having a |
The code would be a lot complexified, not sure it worth it just for an esthetic case... @lyrixx Could we please have your opinion for this and #19247 (comment)? |
Agreed! I'm more concerned about the BC-break. |
{% block form_group_class -%} | ||
col-sm-10 | ||
{%- endblock form_group_class %} | ||
{% block form_group_class %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing space control at the end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space control added.
@@ -100,6 +100,16 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly | |||
); | |||
} | |||
|
|||
public function testLabelNoDivIfFalse() | |||
{ | |||
$this->assertEmpty( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could use a consistent style here:
$view = $this->factory->createNamed('name', 'date', null, array('label' => false))->createView();
$this->assertEmpty($this->renderLabel($view), 'The label should not be rendered');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I honestly don't see the interest. You create a variable that will be used only once.
Why not simply pass the result to the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second that. See more information about thoughtless one-time variables here. Note that OTVs can be used to break down complicated expressions, but I personally don't feel this is complicated enough to warrant an OTV.
If you need the variable because it clarifies things, maybe consider using a comment instead, you can even write whole sentences with them.
@soullivaneuh Looks like there aren't that many things to do to finish this one, can you have a look at the latest comments? Thanks. |
This produces wrong alignments especially for form collections.
@fabpot I corrected the review and answered for this one: #19247 (comment) Also I think I could add a commit to improve a little the process. IMHO, we should introduce some new blocks:
With the current state of this PR, if we want to change the col classes like this: {% use 'bootstrap_3_horizontal_layout.html.twig' %}
{% block form_label_class -%}
col-sm-3
{%- endblock form_label_class %} We will have a broken form like this: So we have to override {% block form_group_class -%}
{%- if label is same as(false) -%}
col-sm-12
{%- else -%}
col-sm-9 {# This is the ONLY real override here #}
{%- endif -%}
{%- endblock form_group_class %} If we have the two blocks I proposed, this will just looks like this (final state): {% use 'bootstrap_3_horizontal_layout.html.twig' %}
{% block form_label_class -%}
col-sm-3
{%- endblock form_label_class %}
{% block form_group_with_label_class -%}
col-sm-9
{%- endblock form_group_with_label_class %} What do you think? Maybe we can find a simplier solution for block name, I waiting your feedbacks for that. But I think we really should avoid the ugly copy/paste constraint. |
2c059f2
to
6efc6b2
Compare
@soullivaneuh would it make sense to only introduce a new block called |
Sorry guys, but I don't have any more time to work on this PR right now. I allowed edit from maintainer, if anyone want to continue it. |
@javiereguiluz @HeahDude Can you take over this one? Or anyone else wanting to take over? |
Closing as there is apparently nobody wanting to take over this PR. Feel free to reopen. |
This produces wrong alignments especially for form collections.
This PR is an alternative to #18504, fixing both #18850 and #18474 issues.
@lyrixx what do you think of this? Maybe I'm forgetting some edge cases, please tell me so we can discuss about it! 👍
cc @jongotlin
Application
With the following form type configuration:
The actual rendering looks like this:
Thanks to this PR, it's now: