Symfony2:日期控件的背景颜色 [英] Symfony2: background color of date widget
问题描述
对于包含日期字段窗口小部件的模板,非日期输入字段将根据CSS更改其背景颜色。在日期窗口小部件上不会发生同样的情况。日期控件的背景颜色应该如何影响? (在表单类中添加attr数组没有效果。)
For a template that includes a date field widget, non-date input fields will have their background colors changed per CSS. The same does not occur on the date widget. How should the background color of the date widget be effected? (Adding an attr array in the form class has no effect.)
(小但真实)差异的屏幕截图:
A screenshot of the (small but real) difference:
代码示例:
模板:
Code samples: Template:
<td>{{ form_widget(form.fname, {'attr': {'class':'smallform'}}) }}
<td>{{ form_widget(form.sname, {'attr': {'class':'smallform'}}) }}
<td>{{ form_widget(form.dateAdded, {'attr': {'class':'smallform'} }) }}
p>
Form class:
->add('fname', null, array('required' => false))
->add('sname', null, array('required' => false))
->add('dateAdded', 'date', array(
'widget' => 'choice',
'format' => 'MM/dd/yyyy',
'pattern' => '{{ year }}-{{ month }}-{{ day }}',
'years' => range(Date('Y'), Date('Y') - 5),
'required' => false,
'data' => date_create(),
))
CSS:
.smallform {
background-color: #f5f5f5;
font-size: 9pt;
color: #000066;
border: 1px solid #CCCC99;
}
推荐答案
解决方案:在我的应用程序的fields.html.twig的自定义副本中修改日期小部件。更精确的解决方案是为此定义一个特定的窗口部件,或找出如何将类属性一直保留到日期窗口部件:
A less-than-subtle solution: modify the date widget in my application's customized copy of fields.html.twig. The more precise solution would be to define a specific widget for this, or figure out how to retain the class attribute all the way into the date widget:
{% block date_widget %}
{% spaceless %}
{% if widget == 'single_text' %}
{{ block('form_widget_simple') }}
{% else %}
<div {{ block('widget_container_attributes') }}>
{{ date_pattern|replace({
'{{ year }}': form_widget(form.year, {'attr': {'class':'smallform'}}),
'{{ month }}': form_widget(form.month, {'attr': {'class':'smallform'}}),
'{{ day }}': form_widget(form.day, {'attr': {'class':'smallform'}}),
})|raw }}
</div>
{% endif %}
{% endspaceless %}
{% endblock date_widget %}
这篇关于Symfony2:日期控件的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!