{# Widgets #}

{% block form_widget_simple %}
{% spaceless %}
    {% set type = type|default('text') %}
    {% if attr.class|default('') == '' %}
        {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
    {% endif %}
    <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock form_widget_simple %}

{% block textarea_widget %}
{% spaceless %}
    {% if attr.class|default('') == '' %}
        {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
    {% endif %}
    <textarea {{ block('widget_attributes') }}>{{ value }}</textarea>
{% endspaceless %}
{% endblock textarea_widget %}
    
{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
        {% if display_inline|default(false) == true %}
            <div class="clearfix">
        {% endif %}
        {% for child in form %}
            {% set _type_class = form.vars.multiple|default(false) ? 'checkbox' : 'radio' %}
            {% if display_inline|default(false) == false %}
                <div class="{{_type_class}}">
                    <label>
                        {{ form_widget(child) }}
                        {% if child.vars.label is not sameas(false) %}
                            {{ child.vars.label|trans({}, translation_domain) }}
                        {% endif %}
                    </label>
                </div>
            {% else %}
                <label class="{{_type_class}}-inline">
                    {{ form_widget(child) }}
                    {% if child.vars.label is not sameas(false) %}
                        {{ child.vars.label|trans({}, translation_domain) }}
                    {% endif %}
                </label>
            {% endif %}
        {% endfor %}
        {% if display_inline|default(false) == true %}
            </div>
        {% endif %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
    
{% block choice_widget_collapsed %}
{% spaceless %}
    {% if attr.class|default('') == '' %}
        {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
    {% endif %}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if empty_value is not none %}
            <option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ empty_value|trans({}, translation_domain) }}</option>
        {% endif %}
        {% if preferred_choices|length > 0 %}
            {% set options = preferred_choices %}
            {{ block('choice_widget_options') }}
            {% if choices|length > 0 and separator is not none %}
                <option disabled="disabled">{{ separator }}</option>
            {% endif %}
        {% endif %}
        {% set options = choices %}
        {{ block('choice_widget_options') }}
    </select>
{% endspaceless %}
{% endblock choice_widget_collapsed %}
    
    
{# Labels #}

{% block form_label %}
{% spaceless %}
    {% if label is not sameas(false) %}
        {% set label_attr = label_attr|merge({'class': (label_attr.class|default('col-xs-12 col-sm-4 control-label')) }) %}
        {% 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('') ~ ' label-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) }} {% if required_indicator|default(required) %}<span class="required">*</span>{% endif %}</label>
    {% endif %}
{% endspaceless %}
{% endblock form_label %}

{% block button_label %}{% endblock %}

{# Rows #}

{% block form_row %}
{% spaceless %}
    <div class="form-group row max-width clearfix {% if errors|length > 0 %}has-error{% endif %}">
        {{ form_label(form) }}
        <div class="{{ container_cls|default('col-xs-12 col-sm-8') }}">
            {{ form_widget(form) }}            
            <div class="form-error-container">{{ form_errors(form) }}</div>
            {% if help|default(null) is not empty %}
                <div class="help-block smaller">{{ help }}</div>
            {% endif %}
        </div>        
    </div>
{% endspaceless %}
{% endblock form_row %}

{% block button_row %}
{% spaceless %}
    <div class="form-group">
        {{ form_widget(form) }}
    </div>
{% endspaceless %}
{% endblock button_row %}

{% block form_errors %}
{% spaceless %}
    {% if errors|length > 0 %}
        {% if form.parent is empty %}
            <div class="alert alert-danger">
                {% for error in errors %}
                    <i class="icon icon-exclamation-sign"></i> {{ error.message }}<br />
                {% endfor %}
            </div>
        {% else %}
            <div class="help-block">
                {% for error in errors %}
                        <i class="icon icon-exclamation-sign"></i> {{ error.message }}<br />
                {% endfor %}
            </div>
        {% endif %}
    {% endif %}
{% endspaceless %}
{% endblock form_errors %}