douzi7219 2016-06-22 11:50
浏览 106
已采纳

在Symfony 2 twig上获取默认日期时间表单

i have some probleme, i would like to have a default value in my datetime twig, i dont want to have the defaut value from BuildForm cause i use it for other twigs.

My add twig

     <div class="form-group">
        {{ form_label(form.Url, "Le URL", {'label_attr': {'class': 'col-sm-4 textTab control-label'}}) }}
        {{ form_errors(form.Url) }}
        <div class="col-sm-6">
          {{ form_widget(form.Url, {'attr': {'class': 'form-control'}}) }}
        </div>
    </div>

    <div class="form-group">
        {{ form_label(form.dateDeLaDemande, "Date de la demande du crawl", {'label_attr': {'class': 'col-sm-4 textTab control-label'}}) }}
        {{ form_errors(form.dateDeLaDemande) }}
        <div class="col-sm-6" style="margin-top: 8px;">
          {{ form_widget(form.dateDeLaDemande, {'attr': {'class': 'col-sm-6'}}) }}
        </div>
    </div>

    <div class="form-group">

        {{ form_label(form.DateDuCrawl, "Date du crawl", {'label_attr': {'class': 'col-sm-4 textTab control-label'}}) }}
        {{ form_errors(form.DateDuCrawl) }}
        <div class="col-sm-6" style="margin-top: 8px;">
          {{ form_widget(form.DateDuCrawl, {'attr': {'class': 'col-sm-6' }}) }}
        </div>
    </div>

And my buildFom

 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
              ->add('site','text')


              ->add('Url','url')
              ->add('dateDeLaDemande','date')
              ->add('DateDuCrawl','date')
              ->add('DateNextCrawl','date')                    
              ->add('faitVP', 'checkbox', array('required' => false))
              ->add('integrerMavec','checkbox', array('required' => false))                    
//            ->add('historique','textarea')
              ->add('historiques', 'collection', array(
                        'type'         => new CategoryType(),
                        'allow_add'    => true,
                        'allow_delete' => true
                      ))
              ->add('save','submit') 
    ;
}

Can some one help me plz ? thanks and sorry about my english :)

  • 写回答

1条回答 默认 最新

  • douhanshu5517 2016-06-22 12:05
    关注

    Try something like this

    {{ form_widget(form.form.dateDeLaDemande, {value : currentDate}) }}
    

    To declare a variable in twig :

    {% set currentDate = "now"|date("m/d/Y")  %}
    

    or directely :

     {{ form_widget(form.form.dateDeLaDemande, {value : "now"|date("m/d/Y")}) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络