问题描述
我正在将 Symfony 项目的 Sonata Admin 使用从 Sonata-Admin 2.2 升级到 2.3,作为将整个项目升级到 Symfony 2.7 的一部分.
I'm upgrading a Symfony project's Sonata Admin usage, from Sonata-Admin 2.2 to 2.3, as part of upgrading the overall project to Symfony 2.7.
我们有许多时间"字段(即,在 Doctrine 中定义为时间",没有有意义的日期组件.)
We have a number of 'time' fields (that is, defined in Doctrine as "time", with no meaningful date component.)
在 Sonata-Admin 2.2 中,FormMapper 的定义很简单:
In Sonata-Admin 2.2 the FormMapper definition was simple:
$formMapper
->tab('tab.general')
->add('start', null, array('label' => 'label.start')
->end()
这给出了半宽度"小时和分钟选择框的布局,并排在表单中:
and that gave the layout of "half-form-width" Hour and Minute selection boxes, side by side, in the form:
但是 Sonata-Admin 2.3 以两行完整的方式显示它们:
But Sonata-Admin 2.3 is showing them over two full lines:
这不太好或不太好用.
那么,我应该设置什么才能获得相同的渲染效果?
So, what should I be setting to get the same rendering?
我尝试过使用sonata_type_datetime_picker",但这确实坚持要显示日期.这些字段没有日期.似乎没有选择时间的等价物.
I've tried using the 'sonata_type_datetime_picker' but that is really insistent on displaying a date. These fields do not have a date. There seems to be no equivalent for just picking the time.
推荐答案
遇到了同样的问题,并通过添加我自己的 CSS 文件进行覆盖来解决.
Had the same issue and solved it by adding my own CSS file for overrides.
1) 将类添加到您的表单字段
1) Add class to your form field
->add('start', 'time', array(
'attr' => array('class' => 'fixed-time')
))
2) 在 Resources/public/css/override.css 下的包中创建一个新的 CSS 文件,并由 fixed-time 类
2) Create a new CSS file in your bundle under Resources/public/css/override.css with overrides by fixed-time class
例如:
.fixed-time .select2-container {
width: auto !important;
}
3) 然后在你的 bundle (Resources/views) 的某个地方创建一个名为 standard_layout.html.twig 的新模板并覆盖 Sonata 模板 + 添加新创建的 CSS 文件(记得先调用 parent(),先加载 Sonata 代码).
3) Then somewhere in your bundle (Resources/views) create a new template called standard_layout.html.twig and override Sonata template + add newly created CSS file (remember to call parent() first, to load sonata code first).
{% extends 'SonataAdminBundle::standard_layout.html.twig' %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{asset('bundles/yourbundle/css/override.css')}}" type="text/css" media="all"/>
{% endblock %}
4) 然后将您的standard_layout twig 模板注册到Sonata admin(在您的config.yml 中).
4) Then register your standard_layout twig template into Sonata admin (in your config.yml).
sonata_admin:
templates:
layout: YourBundle::standard_layout.html.twig
不要忘记安装新资产(assets:install)并清除缓存(从应用/缓存中删除选定的文件夹),否则您将看不到任何变化.
这篇关于Sonata-admin 表单中的时间选择器(无日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!