Skip to content
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

Class on labels not set #237

Closed
CrazyICT opened this issue May 28, 2014 · 4 comments
Closed

Class on labels not set #237

CrazyICT opened this issue May 28, 2014 · 4 comments
Labels
Milestone

Comments

@CrazyICT
Copy link

I've a issue with creating forms. The classes - control-label & col-lg-2 arn't rendered:

    "twig/extensions": "~1.0",
    "knplabs/knp-menu" : "2.0.*@dev",
    "knplabs/knp-menu-bundle":"2.0.*@dev",
    "knplabs/knp-paginator-bundle": "~2.4",
"braincrafted/bootstrap-bundle": "dev-develop",
"twbs/bootstrap": "3.0.*",
    "jquery/jquery":  "1.10.*"

Twig:

{{ form_start(form, { 'style': 'horizontal', 'label_col': 2, 'widget_col': 6 }) }}

{{ form_end(form) }}

output:
label class="required" for="productbundle_product_title"

As you can see, the label class has ony "required"
i expected:

control-label col-lg-2 required

@florianeckerstorfer florianeckerstorfer added this to the 2.1 milestone May 28, 2014
@CrazyICT
Copy link
Author

It seems to be that the file

/vendor/braincrafted/bootstrap-bundle/Braincrafted/Bundle/BootstrapBundle/Resources/views/Form/bootstrap.html.twig

line 510 - 551 is ignored

I tried there to add some text, but never ever it shows up
{% block form_label %}
{% spaceless %}
{% set col_size = col_size|default(bootstrap_get_col_size()) %}

{% if attr.label_col is defined and attr.label_col is not empty %}
    {% set label_col = attr.label_col %}
{% endif %}
{% if attr.widget_col is defined and attr.widget_col is not empty %}
    {% set widget_col = attr.widget_col %}
{% endif %}
{% if attr.col_size is defined and attr.col_size is not empty %}
    {% set col_size = attr.col_size %}
{% endif %}

{% if label is not sameas(false) %}
    {% set style = style|default(bootstrap_get_style()) %}
    {% set label_col = label_col|default(bootstrap_get_label_col()) %}

    {% if attr.style is defined and attr.style is not empty %}
        {% set style = attr.style %}
    {% endif %}

    {% set label_attr = label_attr|merge({ 'class': (label_attr.class|default('') ~ ' control-label')|trim }) %}
    {% if style == 'horizontal' %}
        {% set label_attr = label_attr|merge({ 'class': (label_attr.class|default('') ~ ' col-' ~ col_size ~ '-' ~ label_col)|trim}) %}
    {% elseif style == 'inline' %}
        {% set label_attr = label_attr|merge({ 'class': (label_attr.class|default('') ~ ' sr-only')|trim }) %}
    {% endif %}

    {% if not compound %}
        {% set label_attr = label_attr|merge({'for': id}) %}
    {% endif %}
    {% if required %}
        {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
    {% endif %}
    {% if label is empty %}
        {% set label = name|humanize %}
    {% endif %}
    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain)|raw }}</label>
{% endif %}

{% endspaceless %}
{% endblock form_label %}

@florianeckerstorfer
Copy link
Member

What type of form field are you trying to render? For example, checkbox and radio don't use the default form_label block to render the label (because they are and should be different).

@florianeckerstorfer florianeckerstorfer removed this from the 2.1 milestone Jun 8, 2014
@gbirke
Copy link

gbirke commented Jun 17, 2014

I'll chime in because I have a similar problem - I have a choice element that is rendered as radio buttons. However, the label_attr class is ignored:

{{ form_widget(form.bookType, {label:"Book Type", "label_attr": {"class":"inline"}}) }}

creates

<label for="form_bookType_0" class="required">
<input type="radio" id="form_bookType_0" name="form[bookType]" required="required" value="1">
                    First type
                </label>
...

while i'd expect the class for label to be required inline. See this http://stackoverflow.com/a/18398943/130121 for default Symfony behavior.

@gbirke
Copy link

gbirke commented Jul 27, 2014

I found the source of this error, it's choice_widget_expanded. If you want to have the label_attr on all choice labels, you need to pass it along like this:

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {% if form.multiple is defined %}
            {{ checkbox_row(child, { 'no_form_group': true, "label_attr": label_attr }) }}
        {% else %}
            {{ radio_row(child, { 'no_form_group': true, "label_attr": label_attr }) }}
        {% endif %}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants