首页 文章

Sonata-admin表单中的时间选择器(无日期)

提问于
浏览
2

我正在升级Symfony项目的Sonata Admin使用,从Sonata-Admin 2.2升级到2.3,作为将整个项目升级到Symfony 2.7的一部分 .

我们有许多“时间”字段(即在Doctrine中定义为“时间”,没有有意义的日期组件 . )

在Sonata-Admin 2.2中,FormMapper定义很简单:

$formMapper
   ->tab('tab.general')
   ->add('start', null, array('label' => 'label.start')
   ->end()

并且以下列形式给出了“半形宽度”小时和分钟选择框的布局:

enter image description here

但Sonata-Admin 2.3正在向他们展示超过两行:

enter image description here

这不是那么好或可用 .

那么,我应该设置什么来获得相同的渲染?

我已经尝试过使用'sonata_type_datetime_picker',但这确实是在坚持显示日期 . 这些字段没有日期 . 只是选择时间似乎没有等价物 .

1 回答

  • 2

    有同样的问题,并通过添加我自己的CSS文件来覆盖它来解决它 .

    1)将类添加到表单域

    ->add('start', 'time', array(
        'attr' => array('class' => 'fixed-time')
    ))
    

    2)在Resources / public / css / override.css下的bundle中创建一个新的CSS文件,并通过固定时间类进行覆盖

    例如:

    .fixed-time .select2-container {
        width: auto !important;
    }
    

    3)然后在你的bundle(Resources / views)中的某个地方创建一个名为standard_layout.html.twig的新模板,并覆盖Sonata模板添加新创建的CSS文件(记得先调用parent(),先加载奏鸣曲码) .

    {% 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中) .

    sonata_admin:
        templates:
            layout: YourBundle::standard_layout.html.twig
    

    Do not forget to install new assets (assets:install) and clear cache (remove selected folder from app/cache), otherwise you will not see any change.

相关问题