2015-12-11 58 views
0

我已經在命令工具doctrine中創建了一個實體,並帶有日期字段。現在,通常它會向您顯示一個簡單的選擇框,並且工作正常。現在我想把它作爲日曆,日期選擇器呈現出來。不幸的是,我無法正確理解,即使在stackoverflow上有很多問題和答案。我受夠了Jquery的日期選擇器嘗試過,但它仍然是一個文本框,我會告訴你我的代碼:如何獲取日期在Symfony2表單構建器中的Datepicker

ReserverenType.php

<?php 

namespace Codeit\RestaurantBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class ReserverenType extends AbstractType 
{ 
/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('naam') 
     ->add('datum', 'date' ,array(
     'widget'=> 'single_text', 
     'format'=>'yyyy/MM/dd', 
     )) 
     ->add('tijd') 
     ->add('personen') 
     ->add('email') 
     ->add('opmerkingen') 
    ; 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Codeit\RestaurantBundle\Entity\Reserveren' 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'codeit_restaurantbundle_reserveren'; 
} 
} 

base.html.twig

 {% block stylesheets %} 

     {% stylesheets 
     '%kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/bootstrap.css' 
     '%kernel.root_dir%/../vendor/twbs/bootstrap/dist/css/bootstrap-theme.css' 
     '@CodeitRestaurantBundle/Resources/public/css/jquery-ui.min.css' 
     %} 

     <link rel="stylesheet" href="{{ asset_url }}"> 

     {% endstylesheets %} 
    {% endblock %} 
     {% block javascripts %} 

     {% javascripts 

      '%kernel.root_dir%/../vendor/components/jquery/jquery.js' 
      '%kernel.root_dir%/../vendor/twbs/bootstrap/dist/js/bootstrap.js' 
      '@CodeitRestaurantBundle/Resources/public/js/jquery-ui.min.js' 
     %} 

     <script src="{{ asset_url }}"></script> 

     {% endjavascripts %} 
    {% endblock %} 

新.html.twig

{% extends '::base.html.twig' %} 

{% block body -%} 
    <h1>Maak een reservering</h1> 

    {{ form(form) }} 


    <a href='../../restaurant.php'> 
    <div class="terug" style="margin-top: 10px;"> &larr; Terug naar restaurant pagina </div> 
    </a> 


<script type=」text/javascript」> 
    $('.date').datepicker({ 
    showOn: 'button', 
    buttonImageOnly: true, 
    changeMonth: true, 
    changeYear: true, 
    dateFormat: 'dd-mm-yy', 
    yearRange: "-0:+1" 
}); 
</script> 

{% endblock %} 

輸出:

output form

回答

0

隨着你在最後一個例子使用JavaScript,你需要有這個,而不是以前的數據字段的

$builder->add('datum', 'date' ,array( 'widget'=> 'single_text', 'format'=>'yyyy/MM/dd', 'attr' => ['class' => 'date'], ));

+0

仍然有空白的輸入字段。這是否意味着我的jquery不起作用或什麼? – Gijsberts

+0

忘了提@benoît – Gijsberts

相關問題