0

我想知道如何限制 Symfony2 中表单日期时间分钟字段的列表值。解释如下:

当我单击表单中日期时间字段的分钟字段的组合框时,将显示一个列表值,如下面的屏幕截图所示:

在此处输入图像描述

正如您在上面所注意到的,分钟字段列表值是从 00 到 59(因为我将日期时间格式设置为: 'dd/MM/yyyy H:i' )。顺便说一下,这是表单类代码:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

        ->add('title','text')
        ->add('start','datetime',array(
         'input' => 'datetime',

         'format' => 'dd/MM/yyyy H:i',))
        ->add('end','datetime',array(
         'input' => 'datetime',

         'format' => 'dd/MM/yyyy H:i',))

        ->add('location','text')
        ->add('description','text')

    ;
}

这是表单的html代码:

    <html>
    <head>
        <title> Wkayet </title>
         <link rel="shortcut icon" href="{{asset('bundles/ikprojhome/images/icon-WKAYET.png')}}">
        <link rel="stylesheet" type="text/css" href="{{asset('bundles/ikprojhome/css2/css.css')}}"/>
        <script src='{{asset('bundles/ikprojhome/lib/jquery.min.js')}}'></script> 

    </head>
    <body>
    <center>
        <div id="container">
            <div id="header">

            </div>
            <div id="content">
                <table width="100%" height="100%" align="center">
                    <tr>
                        <td>
                            {% for x in groupe%}
   <form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:x['id']})}}' method="POST" {{ form_enctype(form) }} onsubmit="javascript:parent.jQuery.fancybox.close();">
   <!--<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:x['id']})}}' method="POST" {{ form_enctype(form) }} >-->
                                {% endfor %}
                                 {{ form_errors(form) }}
                                <table align="center">
                                    <tr>
                                        <td class="separation"><label for="groupname">Titre</label></td>
                                        <td>
                                     <!--<input id="titre" name="titre" required="required" type="text" size="50"/> -->
                                         <div>
                                            {{ form_errors(form.title) }}

                                            {{ form_widget(form.title) }}
                                           </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="separation"><label for="debut">Début</label></td>
                                        <td><!--<select id="debut" name="debut" class="select"></select>-->
                                            <div>
                                             {{ form_errors(form.start ) }}

                                             {{ form_widget(form.start ) }}
                                            </div>


                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="separation"><label for="fin">Fin</label></td>
                                        <td><!--<select id="fin" name="fin" class="select"></select>-->
                                            <div>
                                             {{ form_errors(form.end ) }}

                                             {{ form_widget(form.end ) }}
                                          </div> 

                                        </td>
                                    </tr>

                                    <tr>
                                        <td class="separation"><label for="lieu">Lieu</label></td>
                                        <td> 

                                         <div>
                                           {{ form_errors(form.location) }}

                                           {{ form_widget(form.location) }}
                                          </div>

                                        </td>
                                    </tr>
                                    <tr>
                                        <td id="description" valign="top" class="separation"><label for="description">Description</label></td>
                                        <td><textarea id="ikproj_groupebundle_eventsgroupe_description" name="ikproj_groupebundle_eventsgroupe[description]" rows="5" cols="40"></textarea> 



                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="center" id="button" valign="bottom"><input class="button" type="submit" value=""/></td>
                                    </tr>
                                </table>
                                         {{form_widget(form._token)}} 
                            </form>
                        </td>
                    </tr>
                </table> 
            </div>
        </div>
    </center>
</body>
</html>

所以,我的问题是:我怎样才能让分钟字段包含一些特定的值?(例如,它只包含这两个值: 00 和 30 )..这可能吗?

4

1 回答 1

1

请注意,SymfonyTimeType基本上是一个增强的选择字段,并且DateTimeType您正在使用它重用TimeType. 因此,您应该能够'minutes'像这样传入参数:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text')
        ->add('start', 'datetime', array(
         'input' => 'datetime',
         'format' => 'dd/MM/yyyy H:i',
         'minutes' => array(
            0,
            30
         )
        ))
        ->add('end', 'datetime', array(
         'input'  => 'datetime',
         'format' => 'dd/MM/yyyy H:i',
         'minutes' => array(
            0,
            30
         )
        ))
        ->add('location', 'text')
        ->add('description', 'text')
    ;
}

这是解释这一点的文档:http: //symfony.com/doc/current/reference/forms/types/time.html#minutes

于 2014-09-01T10:02:00.457 回答