Form theming datetime widget Symfony 2

后端 未结 1 2007
情话喂你
情话喂你 2021-01-11 11:12

I like to \'form-theme\' my datetime widget in my form. I have created a fields.html.twig file with this in it:

{% block datetime_widget %}
{% spaceless %}         


        
相关标签:
1条回答
  • 2021-01-11 11:57

    To achieve this you must redefine either the datetime_widget without using the built in date_widget & time_widget (because they are each wrapped with divs) or you can redefine the date_widget & time_widget to remove their wrapping divs.

    Here's how to implement the first option:

    {% extends 'form_div_layout.html.twig' %}
    
    {% block datetime_widget %}
    {% spaceless %}
        {% if widget == 'single_text' %}1
            {{ block('field_widget') }}
        {% else %}
            <div {{ block('widget_container_attributes') }}>
                {{ form_errors(form.date.year) }}
                {{ form_errors(form.date.month) }}
                {{ form_errors(form.date.day) }}
                {{ form_errors(form.time) }}
                {{ form_widget(form.date.year) }}
                {{ form_widget(form.date.month) }}
                {{ form_widget(form.date.day) }}
            {{ form_widget(form.time.hour, { 'attr': { 'size': '1' } }) }}:{{ form_widget(form.time.minute, { 'attr': { 'size': '1' } }) }}
            </div>
        {% endif %}
    {% endspaceless %}
    {% endblock datetime_widget %}
    

    And here's how to implement the second option:

    {% extends 'form_div_layout.html.twig' %}
    
    {% block date_widget %}
    {% spaceless %}
        {% if widget == 'single_text' %}
            {{ block('field_widget') }}
        {% else %}
            {{ date_pattern|replace({
                '{{ year }}':  form_widget(form.year),
                '{{ month }}': form_widget(form.month),
                '{{ day }}':   form_widget(form.day),
            })|raw }}
        {% endif %}
    {% endspaceless %}
    {% endblock date_widget %}
    
    {% block time_widget %}
    {% spaceless %}
        {% if widget == 'single_text' %}
            {{ block('field_widget') }}
        {% else %}
            {{ form_widget(form.hour, { 'attr': { 'size': '1' } }) }}:{{ form_widget(form.minute, { 'attr': { 'size': '1' } }) }}{% if with_seconds %}:{{ form_widget(form.second, { 'attr': { 'size': '1' } }) }}{% endif %}
        {% endif %}
    {% endspaceless %}
    {% endblock time_widget %}
    

    Then the date & time will be together in the same div. Obviously you could add some space between the date and time by adding in your desired html

    Hope that helps!

    0 讨论(0)
提交回复
热议问题