2016-10-16 9 views
3

Form Builderでdate_time_picker型を使用するだけで簡単にフィールドにBootStrapのdateTimePickerを追加するDateTimePickerTypeを作成しようとしています。
残念ながら、いくつかの問題が発生しています。具体的には、symfonyは私はこのエラーを与えている:SymfonyとBootstrap dateTimePicker:文字列が必要です

symfonyの\コンポーネント\バリ\ ConstraintViolation
オブジェクト(symfonyの\コンポーネント\フォーム\形態).children [たstartDate] =オブジェクト(日時) - 2016-10-16T10 :45:00 +によって引き起こさ
0200:
のSymfony \コンポーネント\フォーム\例外の\ TransformationFailedExceptionが
プロパティパス "たstartDate" の値を逆にすることができません:文字列を期待。 原因:
Symfony \ Component \ Form \ Exception \ TransformationFailedException
文字列が必要です。

明らかに私のフォームは、DateTimeオブジェクトを私のコントローラに送信しています。コントローラーは、それをStringに変換する際に問題があります。データベースでは、フィールドの型はdatetimeです。私はリゾルバのために多くの異なるオプションを試してみました

class DateTimePickerType extends AbstractType 
{ 
    /** 
    * @return string 
    */ 
    public function getParent() 
    { 
     return 'datetime'; 
    } 

    /** 
    * @param OptionsResolverInterface $resolver 
    */ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 

     $resolver->setDefaults(
      array(
       'empty_data' => new \DateTime(), 
       'widget' => "single_text", 
       'attr' => array(
        'class' => 'addInput col-xs-12 dateSize noPadding noOutline dateTimePicker', 
       ), 
       'format' => 'YYYY-MM-DD HH:mm', 
       'date_format'=>"dd/MM/yyyy hh:mm", 
       'html5' => false, 
      ) 
     ); 
    } 

    /** 
    * @param FormView $view 
    * @param FormInterface $form 
    * @param array $options 
    */ 
    public function buildView(FormView $view, FormInterface $form, array $options) 
    { 
     $view->vars = array_replace($view->vars, array(
      'empty_data' => $options['empty_data'], 
      'widget' => $options['widget'], 
      'format' => $options['date_format'], 
     )); 
    } 

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

は、ここに私のDateTimePickerTypeです。 "empty_data"で "new \ DateTime()"を削除した場合、フォームにnullという値が送信され、データベースにnullを挿入しようとすると、SQLエラーが発生します。 date_time_picker_script.html.twig

{% block date_time_picker_widget %} 
    <div class='input-group date' id='date-time-picker-{{ id }}'> 
     <input type="text" class="form-control" /> 
     <span class="input-group-addon"><i class="fa fa-clock-o"></i></span> 
    </div> 
    {% include 'VMSFormTypeBundle:Template:date_time_picker_script.html.twig' %} {# link to the direct js script to make it datetimepicker #} 
{% endblock %} 

form_template.html.twig のスニペット

{% autoescape false %} 
    <script type="text/javascript"> 
     $(function() { 
      $('#date-time-picker-{{ id }}').datetimepicker(); 
     }); 
    </script> 
{% endautoescape %} 

フォームビルダのスニペット:

$builder 
->add('startDate','date_time_picker') 
事前にの

おかげで

EDIT:ちょうどフォームで送信された日時は、私が選択した内容は無視され、代わりに現在の日時(現在の日付、時間と分)を送信気づきました。

答えて

1

明らかに、これは、PHPとJSの間で時刻形式を変換する必要があるためです。私はDateTypeだけを使用して問題を解決しました(時間は必要ではありませんでした)。

これはまだテストしていないので、まだテストしていない人にとっては便利です。https://github.com/stephanecollot/DatetimepickerBundle

関連する問題