2014-09-01 16 views
0

我想知道如何限制在Symfony2中的窗體日期時間分鐘字段的列表值。對此的解釋是如下:如何在Symfony2中限制窗體日期時間分鐘字段的列表值?

當我在窗體點擊時間字段的分鐘字段的組合框,列表值將顯示,你可以在下面的截圖中看到:

enter image description here

正如你可以注意到上面,分鐘字段列表值是從00到59(因爲我把日期時間格式爲:'dd/MM/yyyy H:我')。順便說一句,這是窗體類代碼:

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)。是否有可能這樣做?

回答

1

請注意,Symfony的TimeType基本上是一個增強的選擇字段,並且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

+1

非常感謝老總KIX,它的工作:)! – 2014-09-01 10:43:56

+1

@NadimAkram,很高興聽到:) – kix 2014-09-01 10:44:10

相關問題