{% use "form_div_layout.html.twig" %}

{# Widgets #}

{% block form_widget %}
{% spaceless %}
    {% if compound %}
        {{ block('form_widget_compound') }}
    {% else %}
        {{ block('form_widget_simple') }}
    {% endif %}
{% endspaceless %}
{% endblock form_widget %}

{% block form_widget_simple %}
{% spaceless %}
    {% set style = style|default(bootstrap_get_style()) %}
    {% set col_size = col_size|default(bootstrap_get_col_size()) %}

    {% if simple_col is not defined and bootstrap_get_simple_col() %}
        {% set simple_col = bootstrap_get_simple_col() %}
    {% endif %}
    {% if attr.simple_col is defined and attr.simple_col is not empty %}
        {% set simple_col = attr.simple_col %}
    {% endif  %}
    {% if attr.col_size is defined and attr.col_size is not empty %}
        {% set col_size = attr.col_size %}
    {% endif %}
    {% if attr.style is defined and attr.style is not empty %}
        {% set style = attr.style %}
    {% endif %}

    {% if simple_col is defined and simple_col %}
        <div class="col-{{ col_size }}-{{ simple_col }}">
    {% endif %}

    {% set type = type|default('text') %}
    {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control')|trim }) %}

    {% if style == 'inline' and (attr.placeholder is not defined or attr.placeholder is empty) %}
        {% if label is empty %}
            {% set attr = attr|merge({ 'placeholder': name|humanize }) %}
        {% else %}
            {% set attr = attr|merge({ 'placeholder': label}) %}
        {% endif %}
    {% endif %}

    <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}>

    {% if simple_col is defined %}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock form_widget_simple %}

{% block form_widget_compound %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
        {% if form.parent is empty %}
            {{ block('global_form_errors') }}
        {% endif %}
        {{ block('form_rows') }}
        {{ form_rest(form) }}
    </div>
{% endspaceless %}
{% endblock form_widget_compound %}

{% block collection_widget %}
{% spaceless %}
    {% if prototype is defined %}
        {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
    {% endif %}
    {{ block('form_widget') }}
{% endspaceless %}
{% endblock collection_widget %}

{% block bootstrap_collection_widget %}
{% spaceless %}
    {% if prototype is defined %}
        {% set prototype_vars = {} %}
        {% if style is defined %}
            {% set prototype_vars = prototype_vars|merge({'style': style}) %}
        {% endif %}
        {% set prototype_html = '<div class="col-xs-' ~ form.vars.sub_widget_col ~ '">' ~ form_widget(prototype, prototype_vars) ~ '</div>' %}
        {% if form.vars.allow_delete %}
            {% set prototype_html = prototype_html ~ '<div class="col-xs-' ~ form.vars.button_col ~ '"><a href="#" class="btn btn-danger btn-small" data-removefield="collection" data-field="__id__">' ~ form.vars.delete_button_text|trans({}, translation_domain) ~ '</a></div>' %}
        {% endif %}
        {% set prototype_html = '<div class="row">' ~ prototype_html ~ '</div>' %}

        {% set attr = attr|merge({'data-prototype': prototype_html }) %}
        {% set attr = attr|merge({'data-prototype-name': prototype_name }) %}
    {% endif %}
    <div {{ block('widget_container_attributes') }}>
        <ul class="bc-collection list-unstyled">
            {% for field in form %}
                <li>
                    <div class="row">
                        <div class="col-xs-{{ form.vars.sub_widget_col }}">
                            {{ form_widget(field) }}
                            {{ form_errors(field) }}
                        </div>
                        {% if form.vars.allow_delete %}
                            <div class="col-xs-{{ form.vars.button_col }}">
                                <a href="#" class="btn btn-danger btn-small" data-removefield="collection" data-field="{{ field.vars.id }}">{{ form.vars.delete_button_text|trans({}, translation_domain) }}</a>
                            </div>
                        {% endif %}
                    </div>
                </li>
            {% endfor %}
        </ul>
        {% if form.vars.allow_add %}
            <a href="#" class="btn btn-primary btn-small" data-addfield="collection" data-collection="{{ form.vars.id }}" data-prototype-name="{{ prototype_name }}">{{ form.vars.add_button_text|trans({}, translation_domain) }}</a>
        {% endif %}
    </div>
{% endspaceless %}
{% endblock bootstrap_collection_widget %}

{% block textarea_widget %}
{% spaceless %}
    {% set col_size = col_size|default(bootstrap_get_col_size()) %}

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

    {% if simple_col is defined %}
        <div class="col-{{ col_size }}-{{ simple_col }}">
    {% endif %}

    {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control')|trim }) %}

    <textarea {{ block('widget_attributes') }}>{{ value }}</textarea>

    {% if simple_col is defined %}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock textarea_widget %}

{% block file_widget %}
{% spaceless %}
    {% set col_size = col_size|default(bootstrap_get_col_size()) %}

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

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

    {% if simple_col is defined %}
        <div class="col-{{ col_size }}-{{ simple_col }}">
    {% endif %}

    <input type="file" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}>

    {% if simple_col is defined %}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock file_widget %}

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        {{ block('choice_widget_expanded') }}
    {% else %}
        {{ block('choice_widget_collapsed') }}
    {% endif %}
{% endspaceless %}
{% endblock choice_widget %}

{% 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 }) }}
        {% else %}
            {{ radio_row(child, { 'no_form_group': true }) }}
        {% endif %}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

{% block choice_widget_collapsed %}
{% spaceless %}
    {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-control')|trim }) %}

    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if empty_value is not none %}
            <option {% if required %} disabled="disabled"{% if value is empty %} selected="selected"{% endif %}{% else %} value=""{% 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 %}

{% block choice_widget_options %}
{% spaceless %}
    {% for group_label, choice in options %}
        {% if choice is iterable %}
            <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                {% set options = choice %}
                {{ block('choice_widget_options') }}
            </optgroup>
        {% else %}
            <option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}

{% block checkbox_row %}
{% spaceless %}
    {% set style = style|default(bootstrap_get_style()) %}
    {% 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 attr.style is defined and attr.style is not empty %}
        {% set style = attr.style %}
    {% endif %}

    {% set class = '' %}
    {% if align_with_widget is defined or attr.align_with_widget is defined %}
        {% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}
        {% set label_col = label_col|default(bootstrap_get_label_col()) %}
        {% set class = 'col-' ~ col_size ~ '-' ~ widget_col ~ ' col-' ~ col_size ~ '-offset-' ~ label_col %}
        <div class="form-group {% if not form.vars.valid %} has-error{% endif %}">
            <div class="{{ class }}">
    {% elseif no_form_group is not defined or no_form_group == false %}
        <div class="form-group{% if not form.vars.valid %} has-error{% endif %}">
    {% endif %}
        <div class="checkbox">
            {% if label is not sameas(false) %}
                {% 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 %}>
                    {{ block('checkbox_widget') }}
                    {{ label|trans({}, translation_domain) }}
                </label>
            {% else %}
                {{ block('checkbox_widget') }}
            {% endif %}
            {{ form_errors(form) }}
        </div>
    {% if align_with_widget is defined or attr.align_with_widget is defined %}
        </div>
        </div>
    {% elseif no_form_group is not defined or no_form_group == false %}
        </div>
    {% endif %}

    {% if style == 'inline' %}&nbsp;{% endif %}
{% endspaceless %}
{% endblock checkbox_row %}

{% block radio_row %}
{% spaceless %}
    {% set class = '' %}

    {% 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 align_with_widget is defined or attr.align_with_widget is defined %}
        {% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}
        {% set label_col = label_col|default(bootstrap_get_label_col()) %}
        {% set class = ' col-'~ col_size ~ '-' ~ widget_col ~ ' col-' ~ col_size ~ '-offset-' ~ label_col %}
        <div class="form-group {% if not form.vars.valid %} has-error{% endif %}">
            <div class="{{ class }}">
    {% elseif no_form_group is not defined or no_form_group == false %}
        <div class="form-group{% if not form.vars.valid %} has-error{% endif %}">
    {% endif %}

        <div class="radio">
            {% if label is not sameas(false) %}
                {% 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 %}>
                    {{ block('radio_widget') }}
                    {{ label|trans({}, translation_domain) }}
                </label>
            {% else %}
                {{ block('radio_widget') }}
            {% endif %}
            {{ form_errors(form) }}
        </div>
    {% if align_with_widget is defined or attr.align_with_widget is defined %}
        </div>
        </div>
    {% elseif no_form_group is not defined or no_form_group == false %}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock radio_row %}

{% block checkbox_widget %}
{% spaceless %}
    <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{% endspaceless %}
{% endblock checkbox_widget %}

{% block radio_widget %}
{% spaceless %}
    <input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{% endspaceless %}
{% endblock radio_widget %}

{% block datetime_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
        {{ block('form_widget_simple') }}
    {% else %}
        {% set attr = attr|merge({ 'class': 'bootstrap-datetime' }) %}
        <div {{ block('widget_container_attributes') }}>
            {{ form_widget(form.date) }}
            {{ form_widget(form.time) }}
            {{ form_errors(form.date) }}
            {{ form_errors(form.time) }}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock datetime_widget %}

{% block date_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
        {{ block('form_widget_simple') }}
    {% else %}
        {% set attr = attr|merge({ 'class': 'bootstrap-date' }) %}
        <div {{ block('widget_container_attributes') }}>
            {{ date_pattern|replace({
                '{{ year }}':  form_widget(form.year),
                '{{ month }}': form_widget(form.month),
                '{{ day }}':   form_widget(form.day),
            })|raw }}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock date_widget %}

{% block time_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
        {{ block('form_widget_simple') }}
    {% else %}
        {% set vars = widget == 'text' ? { 'attr': { 'size': 1 }} : {} %}
        {% set attr = attr|merge({ 'class': 'bootstrap-time' }) %}
        <div {{ block('widget_container_attributes') }}>
            {{ form_widget(form.hour, vars) }}
            {% if with_minutes %}:{{ form_widget(form.minute, vars) }}{% endif %}
            {% if with_seconds %}:{{ form_widget(form.second, vars) }}{% endif %}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock time_widget %}

{% block number_widget %}
{% spaceless %}
    {# type="number" doesn't work with floats #}
    {% set type = type|default('text') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock number_widget %}

{% block integer_widget %}
{% spaceless %}
    {% set type = type|default('number') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock integer_widget %}

{% block money_widget %}
{% spaceless %}
    <div class="input-group">
        {{ money_pattern|replace({
            '{{ widget }}': block('form_widget_simple'),
            '{{ tag_start }}': '<span class="input-group-addon">',
            '{{ tag_end }}': '</span>'
        })|raw }}
    </div>
{% endspaceless %}
{% endblock money_widget %}

{% block url_widget %}
{% spaceless %}
    {% set type = type|default('url') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock url_widget %}

{% block search_widget %}
{% spaceless %}
    {% set type = type|default('search') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock search_widget %}

{% block percent_widget %}
{% spaceless %}
    {% set type = type|default('text') %}
    <div class="input-group">
        {{ block('form_widget_simple') }}
        <span class="input-group-addon">%</span>
    </div>
{% endspaceless %}
{% endblock percent_widget %}

{% block password_widget %}
{% spaceless %}
    {% set type = type|default('password') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock password_widget %}

{% block hidden_widget %}
{% spaceless %}
    {% set type = type|default('hidden') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock hidden_widget %}

{% block email_widget %}
{% spaceless %}
    {% set type = type|default('email') %}
    {{ block('form_widget_simple') }}
{% endspaceless %}
{% endblock email_widget %}

{% block button_widget %}
{% spaceless %}
    {% if label is empty %}
        {% set label = name|humanize %}
    {% endif %}
    {% if type is defined and type == 'submit' %}
        {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' btn btn-'~attr.type|default('primary'))|trim }) %}
    {% else %}
        {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' btn btn-'~attr.type|default('default'))|trim }) %}
    {% endif %}
    <button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{% if attr.icon is defined and attr.icon != '' %}{{ icon(attr.icon) }}{% endif %}{{ label|trans({}, translation_domain) }}</button>
{% endspaceless %}
{% endblock button_widget %}

{% block submit_widget %}
{% spaceless %}
    {% set type = type|default('submit') %}
    {{ block('button_widget') }}
{% endspaceless %}
{% endblock submit_widget %}

{% block reset_widget %}
{% spaceless %}
    {% set type = type|default('reset') %}
    {{ block('button_widget') }}
{% endspaceless %}
{% endblock reset_widget %}

{% block form_actions_widget %}
    {% for button in form.children %}
        {{ form_widget(button) }}&nbsp; {# this needs to be here due to https://github.com/twbs/bootstrap/issues/3245 #}
    {% endfor  %}
{% endblock %}

{# Labels #}

{% 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) }}</label>
    {% endif %}
{% endspaceless %}
{% endblock form_label %}

{% block button_label %}{% endblock %}

{# Rows #}

{% block repeated_row %}
{% spaceless %}
    {#
    No need to render the errors here, as all errors are mapped
    to the first child (see RepeatedTypeValidatorExtension).
    #}
    {{ block('form_rows') }}
{% endspaceless %}
{% endblock repeated_row %}

{% block form_row %}
{% spaceless %}
    {% set style = style|default(bootstrap_get_style()) %}
    {% 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 attr.style is defined and attr.style is not empty %}
        {% set style = attr.style %}
    {% endif %}

    {% set label_col = label_col|default(bootstrap_get_label_col()) %}
    {% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}

    <div class="form-group{% if not form.vars.valid %} has-error{% endif %}">
        {% if style == 'horizontal' %}
            {{ form_label(form) }}
            <div class="col-{{ col_size }}-{{ widget_col }}">
                {{ block('form_input_group') }}
                {{ block('form_help') }}
                {{ form_errors(form) }}
            </div>
        {% else %}
            {{ form_label(form) }}
            {{ block('form_input_group') }}
            {{ block('form_help') }}
            {{ form_errors(form) }}
        {% endif %}
    </div>

    {% if style == 'inline' %}&nbsp;{% endif %}
{% endspaceless %}
{% endblock form_row %}

{% block form_input_group %}
{% spaceless %}
    {% if attr.input_group is defined and attr.input_group is not empty %}
        {% set input_group = attr.input_group %}
    {% endif %}
    {% set input_group = input_group|default({}) %}
    {% if input_group is not empty %}
        {% set ig_size_class = '' %}
        {% if input_group.size is defined and input_group.size == 'large' %}
            {% set ig_size_class = ' input-group-lg' %}
        {% endif  %}
        {% if input_group.size is defined and input_group.size == 'small' %}
            {% set ig_size_class = ' input-group-sm' %}
        {% endif  %}
        <div class="input-group{{ ig_size_class }}">
            {% if input_group.prepend is defined and input_group.prepend is not empty %}
                <span class="input-group-addon">{{ input_group.prepend|raw|parse_icons }}</span>
            {% endif %}
            {{ form_widget(form) }}
            {% if input_group.append is defined and input_group.append is not empty %}
                <span class="input-group-addon">{{ input_group.append|raw|parse_icons }}</span>
            {% endif %}
        </div>
    {% else %}
        {{ form_widget(form) }}
    {% endif %}
{% endspaceless %}
{% endblock form_input_group %}

{% block form_help %}
{% spaceless %}
    {% if attr.help_text is defined and attr.help_text is not empty %}
        {% set help_text = attr.help_text %}
    {% endif %}
    {% set help_text = help_text|default('') %}
    {% if help_text is not empty %}
        <span class="help-block">{{ help_text|trans({}, translation_domain) }}</span>
    {% endif %}
{% endspaceless %}
{% endblock form_help %}

{% block button_row %}
{% spaceless %}
    {% set style = style|default(bootstrap_get_style()) %}
    {% 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 attr.style is defined and attr.style is not empty %}
        {% set style = attr.style %}
    {% endif %}

    {% set label_col = label_col|default(bootstrap_get_label_col()) %}
    {% set widget_col = widget_col|default(bootstrap_get_widget_col()) %}

    {% if style == 'horizontal' %}
    <div class="form-group">
        <div class="col-{{ col_size }}-offset-{{ label_col }} col-{{ col_size }}-{{ widget_col }}">
    {% endif %}

    {{ form_widget(form) }}

    {% if style == 'horizontal' %}
        </div>
    </div>
    {% endif %}
{% endspaceless %}
{% endblock button_row %}

{% block hidden_row %}
    {{ form_widget(form) }}
{% endblock hidden_row %}

{% block form_actions_row %}
    {{ block('button_row')  }}
{% endblock %}

{# Misc #}

{% block form %}
{% spaceless %}
    {{ form_start(form) }}
        {{ form_widget(form) }}
    {{ form_end(form) }}
{% endspaceless %}
{% endblock form %}

{% block form_start %}
{% spaceless %}
    {% set method = method|upper %}
    {% if method in ["GET", "POST"] %}
        {% set form_method = method %}
    {% else %}
        {% set form_method = "POST" %}
    {% endif %}

    {% if style is defined %}
        {% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' form-' ~ style)|trim }) %}
        {{ bootstrap_set_style(style) }}
    {% endif %}

    {% if col_size is defined %}
        {{ bootstrap_set_col_size(col_size) }}
    {% endif %}

    {% if widget_col is defined %}
        {{ bootstrap_set_widget_col(widget_col) }}
    {% endif %}

    {% if label_col is defined %}
        {{ bootstrap_set_label_col(label_col) }}
    {% endif %}

    {% if simple_col is defined %}
        {{ bootstrap_set_simple_col(simple_col) }}
    {% endif %}

    {% if attr.role is not defined or attr.role is empty %}
        {% set attr = attr|merge({ 'role': 'form' }) %}
    {% endif %}

    <form  name="{{ form.vars.name }}" method="{{ form_method|lower }}" action="{{ action }}"{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
    {% if form_method != method %}
        <input type="hidden" name="_method" value="{{ method }}" />
    {% endif %}
{% endspaceless %}
{% endblock form_start %}

{% block form_end %}
{% spaceless %}
    {% if not render_rest is defined or render_rest %}
        {{ form_rest(form) }}
    {% endif %}
    </form>
    {% if bootstrap_get_style() %}
        {{ bootstrap_set_style('') }}
    {% endif %}
    {% if bootstrap_get_col_size() %}
        {{ bootstrap_set_col_size('lg') }}
    {% endif %}
    {% if bootstrap_get_widget_col() %}
        {{ bootstrap_set_widget_col(10) }}
    {% endif %}
    {% if bootstrap_get_label_col() %}
        {{ bootstrap_set_label_col(2) }}
    {% endif %}
    {% if bootstrap_get_simple_col() %}
        {{ bootstrap_set_simple_col(false) }}
    {% endif %}
{% endspaceless %}
{% endblock form_end %}

{% block form_enctype %}
{% spaceless %}
    {% if multipart %}enctype="multipart/form-data"{% endif %}
{% endspaceless %}
{% endblock form_enctype %}

{% block global_form_errors %}
    {% if errors|length > 0 %}
        {% set global_errors = true %}
        {{ block('form_errors') }}
    {% endif %}
{% endblock global_form_errors %}

{% block form_errors %}
{% spaceless %}
    {% set global_errors = global_errors|default(false) %}
    {% set style = style|default(bootstrap_get_style()) %}

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

    {% if errors|length > 0 %}
        {% if global_errors %}
            <div class="alert alert-danger">
        {% endif %}
        <ul{% if not global_errors %} class="help-block"{% endif %}>
            {% for error in errors %}
                <li>{{ error.message|trans(error.messageParameters, translation_domain) }}</li>
            {% endfor %}
        </ul>
        {% if global_errors == true %}
            </div>
        {% endif %}
    {% endif %}
{% endspaceless %}
{% endblock form_errors %}

{% block form_rest %}
{% spaceless %}
    {% for child in form %}
        {% if not child.rendered %}
            {{ form_row(child) }}
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock form_rest %}

{# Support #}

{% block form_rows %}
{% spaceless %}
    {% for child in form %}
        {% set childAttr = {} %}
        {% if attr.col_size is defined %}
            {% set childAttr = childAttr|merge({ 'col_size': attr.col_size }) %}
        {% endif %}
        {% if attr.widget_col is defined %}
            {% set childAttr = childAttr|merge({ 'widget_col': attr.widget_col }) %}
        {% endif %}
        {% if attr.label_col is defined %}
            {% set childAttr = childAttr|merge({ 'label_col': attr.label_col }) %}
        {% endif %}
        {% if attr.simple_col is defined %}
            {% set childAttr = childAttr|merge({ 'simple_col': attr.simple_col }) %}
        {% endif %}
        {% if attr.style is defined %}
            {% set childAttr = childAttr|merge({ 'style': attr.style }) %}
        {% endif %}
        {{ form_row(child, childAttr) }}
    {% endfor %}
{% endspaceless %}
{% endblock form_rows %}

{% block widget_attributes %}
{% spaceless %}
    id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
    {% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% elseif attrname in ['input_group'] %}{% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}

{% block widget_container_attributes %}
{% spaceless %}
    {% if attr.style is defined and (attr.style == 'inline' or attr.style == 'horizontal') %}
        {% set attr = attr|merge({ 'class': 'form-'~attr.style~attr.class|default('') }) %}
        {% set attr = attr|merge({ 'style': null }) %}
    {% endif %}
    {% if id is not empty %}id="{{ id }}" {% endif %}
    {% for attrname, attrvalue in attr %}{% if attrvalue is not null %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_container_attributes %}

{% block button_attributes %}
{% spaceless %}
    id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif %}
    {% for attrname, attrvalue in attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %}
{% endspaceless %}
{% endblock button_attributes %}
